Im trying to make a script that I’ve got into a droplet. I’m expecting a folder or series of files to be dropped, and I only want the ones that are packages. I got the list of packages in my non-droplet version like this:
tell application "Finder" to set pkglist to every package in folder targ
As a droplet, how do I achieve the same results, since what I’m getting [I think] is a list of aliases called names?
Yes, the parameter supplied to an ‘open’ handler is a list of alias(es).
In the past, the fastest way to tell if an alias belongs to a folder or a file has always been to coerce it to string. If the string ends with a colon, the item’s a folder or disk; if it doesn’t, the item’s a file. In OS X (in Jaguar, at least), packages are regarded as folders by AppleScript, but as files by the Finder. Thus they’re coerced to string in different ways in the different contexts:
set f to ((path to "apps" folder from System domain as string) & "iPhoto.app") as alias -- a known package
set s to f as string
--> "PowerBook HD:Applications:iPhoto.app:" -- a colon at the end
tell application "Finder" to get item s as string
--> "PowerBook HD:Applications:iPhoto.app" -- no colon at the end
I don’t know if this works in Panther - or if it will still work in the future - but if it does, it could be exploited to identify a package as such:
on open droppedItems
repeat with thisItem in droppedItems
set s to thisItem as string
tell application "Finder" to set fs to item s as string
if fs is not s then
-- Package
else if s ends with ":" then
-- Folder
else
-- File
end if
end repeat
end open
You might also want to take a look at Standard Additions’ “info for”. It might not be as fast as Nigel’s code but it should provide everything you need to distinguish dropped items.
on open droppedItems
repeat with thisItem in droppedItems
set info_ to (info for thisItem)
if package folder of info_ is true then
-- Package
else if folder of info_ is true then
-- Folder
else
-- File
end if
end repeat
end open
Duh! Do you know, that simply never occurred to me? Back in the old days (a year or two ago), one didn’t use ‘info for’ to distinguish between folders and files because it took so long for it to gather all the other information about them, which wasn’t needed. In the case of folders, this included calculating the combined sizes of everything in them. ‘Info for’ still has that overhead with folders and packages today; but in the context of Cody’s droplet - and given today’s faster systems - it should be virtually unnoticeable.
info for works beautifully, thanks for the tip! Although I have another problem. The purpose of the droplet I’m writing is to generate a series of commands that can be executed by do shell script to install all of the packages dropped onto it. I want the first thing that the user sees upon dropping items to be a dialog with the number of items dropped, prompting for an install volume. If I have to check the info on each iteration, it gets harder. I’ll try and restructure what I’ve got… Thanks again!
edit:
Another question: If a folder is dropped, how do i get all of the packages within it recursively?