Get selection from Finder vs from Script Debugger?

display alert (class of item 1 of (get selection) as text)

gives “alias file” from Script Debugger and “«class alia»” from Finder.

Why ?

What do you mean ? “…from Finder”, I presume, means that you ran something similar to the following:

tell application "Finder" to display alert (class ¬
        of item 1 of (get selection) as text)

But what do you mean when you say “…from Script Debugger” ?

In general, however, if the scripting terminology isn’t immediately available to translate the four-character-code into its “natural language” term, then you’ll get back the raw chevron syntax that wraps the Apple event code denoting a particular descriptor. When I say “immediately available”, this can, of course, be the situation if an AppleScript written on one system is opened on another system that doesn’t have a particular scriptable application. However, it also refers to statements that are executed or return results containing AppleScript objects that are defined by a specific scriptable application, but doing so out-of-context, e.g. not inside either a tell application... or a using terms from... code block, or by importing the application’s scripting dictionary into a script object that allows its ascendants to compile Apple event codes defined by that application but only the script object that imported the dictionary and its children will be able to resolve the event codes into semantic terminology.

So, I’d actually expect this line:

tell application "Finder" to display alert (class ¬
        of item 1 of (get selection) as text)

to return a resolved piece of Finder terminology, namely "alias file", as it’s targeting the application. However, I imagine something like this:

tell the current application to display alert (class ¬
        of item 1 of (get selection in the application ¬
        "Finder") as text)

might return "«class alia»".

Also, have a look what happens when you go to compile this script:

         use application "Finder"
         use scripting additions
end script

set the clipboard to the front Finder window's name


[ I can’t test any of this at the moment, so there may be some inaccuracies above. ]

By “from Script Debugger” I mean running that line in SD.

By “from Finder” I mean running that line as an application called from Spotlight for ex.

And, yes, it’s in a tell application "Finder" block. Sorry for not being clear about that.

Then I’m not certain, although I suspect it’s related to what I wrote. The current application object represents an application instance that houses the environment in which the application runs. Perhaps running it from within Finder preloads the Finder’s dictionary, having the effect I attempted to describe above in some way. This is a guess.

I wonder if you can create a similar outcome with a tell app "FastScripts" to... being executed in a FastScripts environment, but it doesn’t feel equivalent to me. ¯_(ツ)_/¯

I ran some tests with the following script while it was open in a Script Debugger and a Script Editor window:

tell application "Finder"
	display alert (class of item 1 of (get selection) as text)
end tell

I selected files of differing types in a Finder window and the script returned (in a dialog alert) the following:

alias - alias file
application - application file
plain text document - document file
pdf document - document file

I then saved the above script as a compiled script and ran the script by way of FastScripts. The results were:

alias - «class alia»
application - «class appf»
plain text document - «class docf»
pdf document - «class docf»

Finally, I saved the above script as an application on the desktop and ran the script by double-clicking on it. It returned «class appf».

I not certain of the reason for the differing results, but I strongly suspect that the answer is the one given by CK.

Thank you all for the explanations/tests.

What I did is this, and it works:

	# if that item is a Finder alias, it looks for the original item
	if (class of item 1 of (get selection) as text) is in {"alias file", "«class alia»"} then
		set mySelection to original item of item 1 of (get selection)
		set mySelection to item 1 of (get selection)
	end if

This is a very-minor point, but it seems that coercing the class of the selected file to text creates an unnecessary issue. The following seems to work regardless of how it is run.

tell application "Finder"
	set myFile to item 1 of (get selection)
	if class of myFile is alias file then set myFile to original item of myFile
end tell

Not minor point ! :slight_smile: Thank you :slight_smile: