I have never previously used the class of an item to determine what type of item a script is looking at. I tried it in a new script and have a question.
This is the script:
tell application "Finder"
update alias somePath
set itemType to class of alias somePath as text
end tell
When I run this in my script editor I get a result of “folder” or “document file”
When I save as an application and run it, I get a result of “«class cfol»” or “«class cdoc»”
I was using a if/then testing for the result, so obviously my script wouldn’t run since it was looking for “folder” or “document file”. I have now saved it as an app testing for “«class cfol»” or “«class cdoc»”, and it works.
My workaround works, but I am looking for the elegant way to handle this. I’m sure it’s something simple and I just don’t get it.
Where does “somePath” come from?
Hi, jswingchun.
Is there any reason why you need to coerce the class to text? If you simply use the keyword as is, any tests you do will work both in Script Editor and when the script is saved as an application:
tell application "Finder"
set itemType to class of item somePath
if (itemType is folder) then
-- It's a folder.
else (if itemType is document file) then
-- It's a document file.
else
-- Etc.
end it
end tell
Thanks Nigel, that is what I was looking for. I coerced it to text because I had tried your solution yesterday on my own but missed one part. I didn’t realize that the if/then had to be handled by the finder too.
I didn’t get that while this works:
tell application "Finder"
set itemType to class of item somePath
if (itemType is folder) then
display dialog "It's a folder"
else if (itemType is document file) then
display dialog "It's a document file"
end if
end tell
This doesn’t:
tell application "Finder"
set itemType to class of item somePath
end tell
if (itemType is folder) then
display dialog "It's a folder"
else if (itemType is document file) then
display dialog "It's a document file"
end if
Thanks again…
jim