Cuando use mi biblioteca de esta manera, applecript no analizará la línea display dialog
, ¡tratando a dialog
como un identificador!
¿Alguien puede explicar por qué esto no se está entendiendo de la manera que yo quiero? ¡Muchas gracias! ** ACTUALIZACIÓN ** Los dos scripts a continuación ahora se compilarán y funcionarán correctamente
use myLib : script "IHDatabase" -- the name of the library
-- UPDATE -- See also my answer below
use scripting additions
-- UPDATE -- without the above statement this script will not compile
set db to IHDatabase of myLib at "~/Desktop/TestDB.db"
-- IHDatabase handler returns an IHDB script object
tell db
add_sql("drop table if exists testtable;")
add_sql("create table testtable (firstname, lastname, country);")
add_sql("insert into testtable values('Ray', 'Barber', 'USA');")
add_sql("insert into testtable values('jj', 'Sancho', 'Spain');")
add_sql("insert into testtable values('Adam', 'Bell', 'Canada');")
add_sql("insert into testtable values('Bruce', 'Phillips', 'USA');")
add_sql("insert into testtable values('Kim', 'Hunter', 'USA');")
add_sql("insert into testtable values('Kevin', 'Bradley', 'USA');")
add_sql("select * from testtable;") -- columns are separated by pipes ("|") in the result.
set testtable_result to run_sql()
log testtable_result & return & return
set title to "So far, the table contents are: " & return & return
display dialog title & testtable_result buttons {"Done"} default button 1 with icon 1
end tell
Pero si lo escribo de esta manera, con un nivel adicional de "nesting", funciona bien:
tell script "IHDatabase" -- the name of the library
set db to IHDatabase at "~/Desktop/TestDB.db"
-- IHDatabase handler returns an IHDB script object
tell db
add_sql("drop table if exists testtable;")
add_sql("create table testtable (firstname, lastname, country);")
add_sql("insert into testtable values('Ray', 'Barber', 'USA');")
add_sql("insert into testtable values('jj', 'Sancho', 'Spain');")
add_sql("insert into testtable values('Adam', 'Bell', 'Canada');")
add_sql("insert into testtable values('Bruce', 'Phillips', 'USA');")
add_sql("insert into testtable values('Kim', 'Hunter', 'USA');")
add_sql("insert into testtable values('Kevin', 'Bradley', 'USA');")
add_sql("select * from testtable;") -- columns are separated by pipes ("|") in the result.
set testtable_result to run_sql()
log testtable_result & return & return
set title to "So far, the table contents are: " & return & return
display dialog title & testtable_result buttons {"Done"} default button 1 with icon 1
end tell
end tell
Para su información, la biblioteca es:
property name : "IHDatabase"
property version : "1.0"
--IHDatabase class
on IHDatabase at dbname
script IHDB
property loc : missing value -- tells SQLite where to put our db if it doesn't exist, identifies it if it does.
property head : missing value -- the opening statement of every future command to our db.
property tail : missing value --ends every query started with "head".
property sql_stmt_list : missing value -- we build up a SQL program here
on init(dbname)
set loc to space & dbname & space
set head to "sqlite3" & loc & quote
set tail to quote
set sql_stmt_list to {}
return me
end init
on add_sql(stmt)
set stmt_space to stmt & space
set sql_stmt_list to sql_stmt_list & stmt_space
end add_sql
on run_sql()
set rows to do shell script head & sql_stmt_list & tail
set sql_stmt_list to {}
return rows
end run_sql
end script
return IHDB's init(dbname)
end IHDatabase