I wrote the following script to get the Finder item class as text for any AppleScript alias. Moreover, the class name in the form of text must be returned exactly, whether the user executes the script from the script editor or as an application.
set ar0 to ((path to applications folder as text) & "Pages.app") as alias
-- or:
-- set ar0 to alias "Apple HD:Applications:Pages.app:"
-- or:
-- set ar0 to choose folder
-- or
-- set ar0 to choose file
set FinderClassAsText to my FinderClassOfAliasAsText:ar0
on FinderClassOfAliasAsText:anAlias
tell application "Finder" to set ar1 to class of (anAlias as specifier)
try
display dialog (constant ar1 of application "Finder")
on error errorMessage
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"make constant ", " into type string"}}
set {errorMessage, AppleScript's text item delimiters} to {text item 2 of errorMessage, ATID}
tell me to activate
display dialog errorMessage
return errorMessage
end try
end FinderClassOfAliasAsText:
The script works fine. But I would like to know if there is a better way to achieve my goal?
The result should be independent from user locale as well.
Tested in Mojave:
set ar0 to (path to application "Pages")
-- or:
-- set ar0 to alias "Apple HD:Applications:Pages.app:"
-- or:
-- set ar0 to choose folder
-- or
-- set ar0 to choose file
set FinderClassAsText to my FinderClassOfAliasAsText:ar0
on FinderClassOfAliasAsText:anAlias
tell application "Finder" to set ar1 to class of item anAlias
set theText to (run script "on run args
tell app \"Finder\" to return args as text -- or args's beginning as text.
end" with parameters {ar1})
display dialog theText
return theText
end FinderClassOfAliasAsText:
Thanks, Nigel,
This works great. Moreover, it does not use a rather strange trick with the first display dialog of my script.
I made only one change to your script: item (anAlias as text). But this is just in case - so that everything is according to the rules for any version of Finder.
set FinderClassAsText to my FinderClassOfAliasAsText:(alias "Apple HD:Applications:Pages.app:")
on FinderClassOfAliasAsText:anAlias
tell application "Finder" to set ar1 to class of item (anAlias as text)
set theText to (run script "on run args
tell app \"Finder\" to return args as text
end" with parameters {ar1})
display dialog theText
return theText
end FinderClassOfAliasAsText:
Another minor variation would be to put getting the class inside the inner script too:
set FinderClassAsText to my FinderClassOfAliasAsText:(alias "Apple HD:Applications:Pages.app:")
on FinderClassOfAliasAsText:anAlias
set theText to (run script "on run args
tell app \"Finder\" to return (class of item (args as text)) as text
end" with parameters {anAlias})
display dialog theText
return theText
end FinderClassOfAliasAsText: