[VApp] How do I import a picture?
David Hood
david.hood at stonebow.otago.ac.nz
Wed May 7 19:30:27 CDT 2003
For the spurious reason no one else seems to have done it, I decided to
work out how to import binary information using only applescript. Many
hours later I present the definitive example script - this creates a
database called 'imagine' on the desktop, prompts for a binary file
(i.e. a picture, image or whatever) to store in the database, stores
the binary data, then draws the file back out of the database and saves
it to the desktop.
Known caveats:
- Since I've used strings for building path info it may do odd things
on non-latinate path/file information.
- Don't use a binary file on the desktop, or indeed have any file there
of the same name as the one you choose (unless you really want the
example to overwrite it).
- Because of quirks associated with applescripts I/O I would not depend
on it using files greater than 20M.
- I have no idea if this is compatible with other ways of working with
valentina's blob fields
- Tested on OS 10.2.5, VAPP 1.9.7b6
- If run in system 9 you may need to increase the memory of script
editor depending on the size of the binary.
- Some of the lines in the script may be word wrapped so that will need
to be fixed when compiling in script editor
Regards,
David Hood
------------------------
set someDatabase to (path to desktop as string) & "Imagine"
CreateDB(someDatabase)
set fileNameForNextPart to exampleAddRecord(someDatabase)
exampleFileExport(fileNameForNextPart, someDatabase)
------------------------
on CreateDB(DBname)
tell application "Valentina Carbon"
set theDB to make new database with data file DBname
tell theDB
SQL Execute "CREATE TABLE binaryFiles (" & ¬
"fileName String(30) NOT NULL," & ¬
"fileType String(4) NOT NULL," & ¬
"creatorType String(4) NOT NULL," & ¬
"binaryData BLOB(512) )"
end tell
end tell
close theDB
end CreateDB
-----------------------
on exampleAddRecord(DBname)
set pathstring to choose file with prompt "Binary file to Input:"
try
set inputFile to open for access pathstring
set fileData to read inputFile as data
close access inputFile
on error errMsg number errNum
try
close access pathstring
end try
error errMsg number errNum
end try
tell application "Finder"
set ctype to creator type of pathstring as string
set ftype to file type of pathstring as string
set fname to name of pathstring as string
end tell
tell application "Valentina Carbon"
set DB to open file DBname
tell DB
set curs to SQL Select "SELECT * FROM binaryFiles"
end tell
make new record with data {fname, ftype, ctype, fileData} at end of
curs
close DB
end tell
return fname
end exampleAddRecord
--------------------
on exampleFileExport(searchCriteria, DBname)
tell application "Valentina Carbon"
set DB to open file DBname
tell DB
set theCursor to SQL Select "SELECT * FROM binaryFiles"
end tell
set fileN to contents of field "fileName" of record 1 of theCursor
set fileT to contents of field "fileType" of record 1 of theCursor
set creatorT to contents of field "creatorType" of record 1 of
theCursor
set binaryD to contents of field "binaryData" of record 1 of theCursor
delete theCursor
close DB
end tell
tell application "Finder"
set fileOutput to (make new file in desktop with properties
{name:fileN, creator type:creatorT, file type:fileT}) as string
end tell
try
set binaryOutFile to open for access file fileOutput with write
permission
set eof of binaryOutFile to 0
write binaryD to binaryOutFile
close access binaryOutFile
on error errMsg number errNum
try
close access file fileOutput
end try
error errMsg number errNum
end try
end exampleFileExport
More information about the Valentina
mailing list