Mystery: error setting alias - but then the alias works as expected


#1

Can anyone help me sort out this slightly baffling question?

I have a script that uses a unix utility to create an .ODT (Open Document Text format) file in the temporary folder, then sets an alias for it, hides the ODT extension, and opens the file in TextEdit. Here is a bit of the code:

tell application "Finder"
		set fileAlias to POSIX file odtPOSIX as alias
	end tell
	
	tell application "Finder"
		try
			set extension hidden of fileAlias to true
		on error
			display dialog "Can't hide odt extension" with title msgTitle buttons {"OK"} giving up after 5
		end try
	end tell
	
	tell application "TextEdit"
		open fileAlias
		activate
		windows where name contains nameOnly
		if result is not {} then set index of item 1 of result to 1
	end tell

But the set fileAlias line produces this error in the log:

get POSIX file “/private/var/folders/sm/04hq0wgx7qg27h7dn93rlgp40000gn/T/TemporaryItems/BIBLIO_I.odt”
Error: no such object (e.g. specifier asked for the 3rd, but there are only 2) (errAENoSuchObject:-1728)

What beginner’s error am I making here?


(CK) #2

I would place the set fileAlias to... outwith any specific tell application... block. So, change:

tell application "Finder"
    set fileAlias to POSIX file odtPOSIX as alias
end tell

to

set fileAlias to POSIX file odtPOSIX as alias

#3

That fixes it perfectly. Thank you!