I am trying to rewrite an InDesign workflow to use CS3 and Database Events instead of useless loops threw a text file but running into issues with unicode text.
My original workflow read a unicode text file using delimiter return and then looped threw each node or line and parsed out values to pass to InDesign. Now I am trying to use the Database Events to convert the text file to sqlLite but having strange results.
–This worked in Tiger
set myData to read myFile as Unicode text using delimiter return
–This should work in Leopard but the data does not delimitate
set myData to read myFile as Unicode text using delimiter {“\n”}
–So I used this as a work around
set myData to read myFile from 3 using delimiter {“\n”}
Using the last read command works and the Event Log shows the data delimitated but when values are parsed out nothing get to the database.
Here is the script
Note I have to give props to Martin Michel for the Database routine, very nice man.
-- author: Martin Michel
-- eMail: martin@joyofscripting
-- created: June 2007
-- requires: Mac OS X 10.4.9
on run
tell application "Finder"
set tag_file to (choose file) as alias
set myFile to open for access tag_file
set myData to read myFile as Unicode text using delimiter {"\n"}
--set myData to read myFile from 3 using delimiter {"\n"}
close access myFile
set myRecName to name of file tag_file as Unicode text
end tell
--Create Database Begin****************************************
setdbname("webshopDB")
setdbpath((path to desktop) as Unicode text)
opendb()
--Create Database End****************************************
set rec to createrec(myRecName)
repeat with aRecord in myData
try
set AppleScript's text item delimiters to {"="}
set myVar to (text item 1 of aRecord as Unicode text)
createfield(rec, myVar, (text item 2 of aRecord as Unicode text))
end try
end repeat
closedb("saving yes")
--Save Database Record****************************************
--Open Database Record****************************************
setdbname("webshopDB")
setdbpath(path to desktop) as Unicode text
opendb()
set rec to getrecbyname(myRecName)
set mystring to getfieldvalue(rec, "<@useremail@>" as Unicode text)
--display dialog getfieldvalue(rec, "<@template@>")
--display dialog getfieldvalue(rec, "<@useremail@>" )
closedb("saving no")
quitdbe()
end run
property db : missing value
property dbname : missing value
property dbpath : missing value
-- sets the name of the database to use
on setdbname(newdbname)
set dbname to newdbname
end setdbname
-- sets the parent folder path of the database to use
-- path must be given in mac style (e.g. "Users:martin:Desktop:")
on setdbpath(newdbpath)
set dbpath to newdbpath
end setdbpath
-- indicates if the database file already exists
on dbpathexists()
set fullpath to (dbpath & dbname & ".dbev" as Unicode text)
try
set fullalias to fullpath as alias
return true
on error
return false
end try
end dbpathexists
-- opens the db/creates a new database if the database
-- file does not already exist
on opendb()
if not dbpathexists() then
tell application "Database Events"
set db to make new database with properties {name:dbname, location:dbpath}
end tell
else
tell application "Database Events"
set db to database (POSIX path of (dbpath & dbname & ".dbev"))
end tell
end if
end opendb
-- saves the database
on savedb()
tell application "Database Events"
save db
end tell
end savedb
-- returns the names of all records in the database
on getrecnames()
tell application "Database Events"
tell db
set recnames to name of every record
return recnames
end tell
end tell
end getrecnames
-- returns the ids of all records in the database
on getrecids()
tell application "Database Events"
tell db
set recids to id of every record
return recids
end tell
end tell
end getrecids
-- creates a record in the database
-- record name must be given
on createrec(recname)
tell application "Database Events"
tell db
set newrec to make new record with properties {name:recname}
return newrec
end tell
end tell
end createrec
-- creates a field with a field value in a record
-- record, field name & field value must be given
on createfield(rec, fieldname, fieldvalue)
tell application "Database Events"
tell db
tell rec
make new field with properties {name:fieldname, value:fieldvalue}
end tell
end tell
end tell
end createfield
-- creates multiple fields with field values in a record
-- record, field names and corresponding field values must be given
on createfields(rec, fieldnames, fieldvalues)
tell application "Database Events"
tell db
tell rec
repeat with i from 1 to length of fieldnames
set fieldname to item i of fieldnames
set fieldvalue to item i of fieldvalues
make new field with properties {name:fieldname, value:fieldvalue}
end repeat
end tell
end tell
end tell
end createfields
-- get all records of the database
on getrecs()
tell application "Database Events"
tell db
set allrecs to every record
return allrecs
end tell
end tell
end getrecs
-- get a record by name
on getrecbyname(recname)
tell application "Database Events"
tell db
set rec to first record whose name is recname
return rec
end tell
end tell
end getrecbyname
-- get a record by id
on getrecbyid(recid)
tell application "Database Events"
tell db
set rec to first record whose id is recid
return rec
end tell
end tell
end getrecbyid
-- returns the record when providing the record name
-- attention: returns only one record id even when
-- several record with the same name exist
on getrecid(recname)
tell application "Database Events"
tell db
set recid to id of first record whose name is recname
return recid
end tell
end tell
end getrecid
-- returns the record name when providing the record id
on getrecname(recid)
tell application "Database Events"
tell db
return name of record recid
end tell
end tell
end getrecname
-- returns the field value of a record field
-- record & field name must be given
on getfieldvalue(rec, fieldname)
tell application "Database Events"
tell db
tell rec
set fieldvalue to value of field fieldname
return fieldvalue
end tell
end tell
end tell
end getfieldvalue
-- sets the field value of a record field
-- record, field name & field value must be given
on setfieldvalue(rec, fieldname, fieldvalue)
tell application "Database Events"
tell db
tell rec
set value of field fieldname to fieldvalue
end tell
end tell
end tell
end setfieldvalue
-- indicates if a given record name exists in the database
on hasrecname(recname)
if recname is in my getrecnames() then
return true
else
return false
end if
end hasrecname
-- indicates if a record contains a given field name
on hasfieldname(rec, fieldname)
tell application "Database Events"
tell db
tell rec
set fieldnames to name of every field
if fieldname is in fieldnames then
return true
else
return false
end if
end tell
end tell
end tell
end hasfieldname
-- deletes a given field of a given record
on delfieldname(rec, fieldname)
tell application "Database Events"
tell db
tell rec
delete field fieldname
end tell
end tell
end tell
end delfieldname
-- delete a complete record in the database
on delrec(rec)
tell application "Database Events"
tell db
delete rec
end tell
end tell
end delrec
-- deletes all records in the database
on delallrecs()
tell application "Database Events"
tell db
delete every record
end tell
end tell
end delallrecs
-- closes the database
-- two modes availabe: close "saving yes" and "saving no"
on closedb(mode)
tell application "Database Events"
if mode is "saving yes" then
close db saving yes
else if mode is "saving no" then
close db saving no
end if
end tell
set db to missing value
set dbame to missing value
set dbpath to missing value
end closedb
-- quits the Database Events application
on quitdbe()
tell application "Database Events"
quit
end tell
end quitdbe
AppleScript: 2.2
Browser: Safari 525.13
Operating System: Mac OS X (10.5)