Alias object's "original item" not working in Automator

I want to use code like this in Automator (v2.8 under 10.13.6):


set theFile to alias "..."
set theFile to theFile's original item

This code works fine in Script Editor, but when I run it in a “Run AppleScript” action in Automator, I get an error (-212) because on the word “item”.

Any idea how to make this work?

The goal is to be able to make a workflow that accepts Finder Aliases and get their targets (from which I then get the bundle id if it’s an application - without using “original item” I’d get “MACS” as id, which is not what I want).

Update: It seems I need to do this in the Finder’s context. At least that’s what testing with fixed alias paths showed. But as soon as I install it as a Service and then use it on a Finder Alias, it fails again. This is so frustrating.

My current automator action code looks like this:

on run {input, parameters}
	set theFile to input
	tell application "Finder"
		try
			set theFile to original item of theFile
		on error
			-- this was no Finder Alias, so we ignore this error
		end try
	end tell
	try
		set theID to id of application (theFile as text)
		display dialog "The bundle ID is:" & return & return & theID buttons "OK" default button "OK"
	on error
		display dialog "This file has no bundle ID" buttons "OK" default button "OK"
	end try
end run

Just as a point of clarification, it’s my understanding that an alias object and an alias file are not the same thing to the Finder and that original item is a property of alias file. I raise this issue because the title of this thread is “Alias object’s original item not working in Automator”.

Good point. Possible this confusion is the cause of my issue.

Hi,

Give this a whirl.

on run {input, parameters}
	tell application "Finder"
		activate
		set chosenFile to file input
		set fileKind to get (class of chosenFile)
		if fileKind is alias file then
			set originalFile to original item of chosenFile as alias
			if class of file originalFile is application file then
				set bundleID to id of application file originalFile
				display dialog "The bundle ID is: " & bundleID
			else
				display dialog "Original file has no bundle ID."
			end if
		end if
	end tell
	return input
end run

Cheers,

H

Thank you. However, that code still doesn’t work with all kinds of items (folders, files, and Finder Aliases to either), because “file …” will throw an error if it’s a folder, for instance.

Also, “input” is of type “alias” when it received items as an Automator action.

Here’s my updated version (again with “try” because I don’t see how to check for the various cases better):

on run {input, parameters}
	tell application "Finder"
		activate
		set theTarget to input -- default case
		try -- check if it's an alias, and resolve it
			set theFile to file (input as text)
			set fileKind to get (class of theFile)
			if fileKind is alias file then
				set theTarget to original item of theFile as alias
			end if
		end try
		set fileName to name of (theTarget as alias)
		try
			set isApp to class of file theTarget is application file
		on error
			set isApp to false
		end try
		if isApp then
			set bundleID to id of application file theTarget
			display dialog "The bundle ID of " & fileName & " is:" & return & return & bundleID buttons "OK" default button "OK"
		else
			display dialog fileName & " has no bundle ID" buttons "OK" default button "OK"
		end if
	end tell
end run

Hopefully this is an improvement on my original. It treats the input as a Finder item, rather than expecting it to be a file; it passes over anything that isn’t an alias (document files, folders, disks etc), it handles multiple selections, and it offers a bit more info about what has been selected.

on run {chosenItems}
	repeat with eachItem in chosenItems
		tell application "Finder"
			activate
			set itemName to name of eachItem
			set fileKind to class of item eachItem
			if fileKind is alias file then
				set originalFile to original item of eachItem as alias
				if class of file originalFile is application file then
					set bundleID to id of application file originalFile
					display dialog "The bundle ID of the original file of the alias file " & itemName & " is: " & bundleID
				else
					display dialog "The original file of the alias file " & itemName & " has no bundle ID."
				end if
			else
				display dialog itemName & " isn't an alias file."
			end if
		end tell
	end repeat
end run

The input parameter of Automator.app is always list of aliases. So, when you refer to input, you should get some item of input:

set theAlias to item 1 of input

Thomas, you can use ASObjC:

use framework "Foundation"
use scripting additions

on run {input, parameters}
	set theURL to (current application's NSArray's arrayWithArray:input)'s firstObject()
	set {boolResult, theValue} to theURL's getResourceValue:(reference) forKey:(current application's NSURLIsAliasFileKey) |error|:(missing value)
	if theValue as boolean is true then
		set {theURL, theError} to current application's NSURL's URLByResolvingAliasFileAtURL:theURL options:0 |error|:(reference)
	end if
	if theURL is not missing value then
		set {boolResult, theValue} to theURL's getResourceValue:(reference) forKey:(current application's NSURLIsApplicationKey) |error|:(missing value)
		if theValue as boolean is true then
			set theID to (current application's NSBundle's bundleWithURL:theURL)'s bundleIdentifier()
			display dialog "The bundle ID is:" & return & return & (theID as text) buttons "OK" default button "OK"
			return
		end if
	end if
	display dialog "This file has no bundle ID" buttons "OK" default button "OK"
end run