Finder: make alias file ... but document file returned

I have troubles creating alias files and using them straight away

first I used the following snippet to make the alias


		make new alias file at myTarget to originalItem
		copy result to newAliasInTarget

after which sometimes the following line would throw an error

log newAliasInTarget's class

the error reads:

so I changed to this snippet to make the alias and assign it to a variable


		make new alias file at myTarget to originalItem
		copy result as alias to newAliasInTarget

but that just makes the error message wierder and come a little earlier

Here’s the situation densed down to the problem part:

tell application "Finder"
	-- setup parameters
	set mySelection to the selection
	set myTarget to make new folder at desktop
	
	-- loop thru using index to simulate the real situation
	repeat with i from 1 to mySelection's length
		-- get the original Finder item from selection
		set originalItem to mySelection's item i
		-- make a new 'alias file' and assign it to variable
		make new alias file at myTarget to originalItem --> this mostly takes a long time! Why?
		
		-- this didn't work for 100% of the test cases
		--copy result to newAliasInTarget
		
		-- so I tried:
		copy result as alias to newAliasInTarget --> and it worked for the same files (sometimes)
		(* on re-runing again with the same selection still in place, I got this error:
		Can't make «class docf» "Picture 29.png" of «class cfol» "untitled folder 13" of «class cfol» "Desktop" of «class cfol» "swissalps" of «class cfol» "Users" of «class sdsk» of application "Finder" into type alias
		what also confuses me, is that the whole loop is in a tell "Finder" block, but still the error message uses the <<class xxx>> syntax
		*)
		-- disk space looks ok
		
		-- now comes the testing and possible error
		log newAliasInTarget's class --> should be: 'alias file'
		(* but often times I get an error message:
		Finder got an error: Can't get document file "Picture 27.png" of folder "untitled folder 11" of folder "Desktop" of folder "swissalps" of folder "Users" of startup disk
		*)
	end repeat
end tell

The real brain racker is that sometimes it works or it works for some items and others not.
When testing I have the same selection of the same items in the same window on every run.
All the test files are ‘clean’ alias files I made by dragging and dropping holding the required keys.
The originalItem’s targets are all png files in a folder on the desktop with standard user permissions, nothing locked nothing opened. It’s just my script that uses them.

After restarting Script Editor.app, the chances of success seem a little higher for the first round.

what am I doing wrong?

Thanks in advance for any help or idea.

Hi,

with Script Debugger it works always without any problems.
I just changed the lines for cosmetic reason


.
copy class of result to newAliasInTarget
.
log newAliasInTarget --> should be: 'alias file'
.

but this doesn’t change the process to create the alias files

Does the script work running it as application or as script from script menu?

Hi StefanK

sorry, I just realize how misleading shorthand AppleScript can be.

It’s not the class I want to store in newAliasInTarget but to see the class of newAliasInTarget
I would like to use newAliasInTarget later, after the log line, to do further operations on it. eg set it’s position, label etc.

to cosmetically transform my shorthand line


log newAliasInTarget's class

it would read:


log (class of newAliasInTarget)

I’m just logging the class of newAliasInTarget to check that it is in fact an ‘alias file’ and not a ‘document file’

Also I would like to emphasize that it sometimes works on some files and sometimes won’t using the exact same files!

Thanks for testing. Maybe It’s my system only that has this problem.

here’s the full test-case-script as originally posted but with shorthands removed


tell application "Finder"
	-- setup parameters
	set mySelection to the selection
	set myTarget to make new folder at desktop
	
	-- loop thru using index to simulate the real situation
	repeat with i from 1 to length of mySelection
		-- get the original Finder item from selection
		set originalItem to item i of mySelection
		-- make a new 'alias file' and assign it to variable
		make new alias file at myTarget to originalItem --> this mostly takes a long time!
		
		-- this didn't work for 100% of the test cases
		--copy result to newAliasInTarget
		
		-- so I tried:
		copy (result as alias) to newAliasInTarget --> and it worked for the same files (sometimes)
		(* on re-runing again with the same selection still in place, I got this error:
		Can't make «class docf» "Picture 29.png" of «class cfol» "untitled folder 13" of «class cfol» "Desktop" of «class cfol» "swissalps" of «class cfol» "Users" of «class sdsk» of application "Finder" into type alias
		what also confuses me, is that the whole loop is in a tell "Finder" block, but still the error message uses the <<class xxx>> syntax
		*)
		-- disk space looks ok
		
		-- now comes the testing and possible error
		log (class of newAliasInTarget) --> should be: 'alias file'
		(* but often times I get an error message:
		Finder got an error: Can't get document file "Picture 27.png" of folder "untitled folder 11" of folder "Desktop" of folder "swissalps" of folder "Users" of startup disk
		*)
	end repeat
end tell

I still wonder why Finder sometimes returns a ‘document file’ instead of an ‘alias file’ when making alias files
Has this occurred to anyone yet?

Your script script also works OK for me (no errors; the logged classes are always or alias (or alias file if I revert to the commented-out variation). I am using plain Script Editor on Tiger.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 4 Public Beta (4528.17)
Operating System: Mac OS X (10.4)

Thanks Chris,

I assume you did multiple re-runs… as that is part of the problem.

The first variation is definitely what I am looking for. But that never worked on my system even after restarting Finder

  • AppleScript 2.0.1 - Script Editor Version 2.2.1 (100.1) - OS X 10.5.x -

I am sure to have found the source of the errors.
Thanks to all who helped on this issue.:cool:

Narrowing down the apps that could interfere and rebooting etc. I came to understand that a virus scanner can
have it’s downsides.
Apparently the Applescript refrence to the alias file is damaged by the scanner checking the newly created alias file.
The scanner changes the files it scans, even if it’s only a last opened flag.
Applescript can’t find the file anymore.

my testing script is running repeatedly on the same selection as I type. It’s done over 120 repetitions on up to 20 files.
Still no error, as long as I keep the scanner from visiting my sandbox.

And that is the solution to my problem: Alert user of this incident, should it occur, and request a path to a sandbox (or to ask user to turn of the interfering application).