Hi, I need to get the creater code from a file dropped on my applescript studio application.
Right now I do it this way;
on drop theObject drag info dragInfo
set bolval to false
set dataTypes to types of pasteboard of dragInfo
if "file names" is in dataTypes then
set theFiles to {}
set preferred type of pasteboard of dragInfo to "file names"
-- Get the list of files from the pasteboard
set theFiles to contents of pasteboard of dragInfo
set appalias to (((first item of theFiles) as POSIX file) as alias)
tell application "Finder" to set appcode to creator type of appalias
if appcode is "1234" then
display dialog "seems ok"
set bolval to true
else
display dialog "This doesn't appear to be a the right application"
end if
end if
-- Set the preferred type back to the default
set preferred type of pasteboard of dragInfo to ""
return bolval
end drop
This works, but it does the job awefully slow! the slow part seems to be the tell application “Finder” to set appcode to creator type of appalias statement.
Anyone have a better and faster solution. applescript or objective C, it doesn’t matter.
set Chosen to (choose file)
tell application "Finder"
set FT to file type of file Chosen
set CT to creator type of file Chosen
return {FT, CT}
end tell
While this works if the file does have a creator type and file type, it won’t work for lots of files in OS X that don’t. In those cases, it returns {missing value, missing value}, where as for a BBEdit document for example, it returns {“TEXT”, “R*ch”}
I clocked my own and the solution suggested above.
Both take a litte more then 25 seconds to execute.
I guess I need to go objective C to solve this faster.
Any more suggestions?
I think it’s not good to do things with the Finder in the drop handler, because there is a connection with the Finder on dropped items. A better strategy would be to set a flag in the drop handler. Something like this:
on drop theObject drag info dragInfo
set dataTypes to types of pasteboard of dragInfo
if “file names” is in dataTypes then
set theFiles to {}
set preferred type of pasteboard of dragInfo to “file names”
set theFiles to contents of pasteboard of dragInfo
end if
set items_dropped to theFiles is not {}
return true
end drop
If theFiles is not {}, then items_dropped will be true. Then in an idle handler, check for the items_dropped flag.
on idle theObject
if items_dropped then
set first_item to item 1 of theFiles
set item_reference to (first_item as POSIX file) as alias
tell application “Finder”
set ct to creator type of item_reference
end tell
set items_dropped to false
log ct
end if
return 1
end idle
Notice that here, there is no problem using the Finder since it is not in the drop handler which already return true.
A lot of people have been posting problems with parts of their programs taking too long in the handlers. It’s better to not do these long programs in the handlers keeping them free for other parts of the ui.