Saturday, December 3, 2022

#1 2022-09-30 10:36:50 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

Get Finder class of an alias as Text and display it

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.

Applescript:


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.

Last edited by KniazidisR (2022-09-30 10:37:36 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#2 2022-10-01 05:45:42 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5588

Re: Get Finder class of an alias as Text and display it

Tested in Mojave:

Applescript:


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:


NG

Offline

 

#3 2022-10-01 02:55:47 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

Re: Get Finder class of an alias as Text and display it

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.

Applescript:


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:

Last edited by KniazidisR (2022-10-01 03:06:06 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#4 2022-10-01 03:49:30 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5588

Re: Get Finder class of an alias as Text and display it

Another minor variation would be to put getting the class inside the inner script too:

Applescript:


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:


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)