Detecting an 'Alias' or a Symlink

I’m creating a script that upon an item being added it checks its size and if it’s over a certain amount it moves it to a different drive and creates a symlink to the folder. (this script will be used in /Applications/)

This is so I’m able to save space on my SSD and run other applications on an HDD (while having everything in one folder for simplicity)

The problem I run into is that after I move the item and create a symlink, it detects the alias/symlink itself as a newly addedItem and therefore runs the script. I created a ‘check’ so that it’ll ignore alias’ but instead of checking the alias itself, it goes to the path and checks the ‘kind’ of that instead. Resulting in the same item I just added.

This is my script (sorry in advance for the messiness)



on adding folder items to thisFolder after receiving addedItems
	set toolbox to load script alias ¬
		((path to library folder from user domain as string) ¬
			& "Scripts:toolbox.scpt")
	
	log_event("Init") of toolbox
	
	
	
	set myStorage to "Storage HDD:Applications"
	
	log_event(addedItems) of toolbox
	tell application "Finder"
		-- Count how many items added
		set addedAmount to count addedItems
		log_event(addedAmount) of toolbox
		
		repeat with addedItem in addedItems
			log_event(addedItem) of toolbox
			
			if kind of addedItem as string is "Alias" then
				log_event("File is an alias, ignored") of toolbox
			else
				log_event(quoted form of (kind of addedItem as string)) of toolbox
				-- Save the size of the added item
				set mySize to (size of (info for addedItem)) / 1024 as integer
				log_event(mySize) of toolbox
				
				-- If it's too big; create an symlink
				if mySize is greater than 500000 then
					log_event("Over 500mb...") of toolbox
					try
						-- Grab the filename
						tell (info for addedItem) to set myFilename to name
						
						-- Record previous location (this is used to create the symlink later)
						set oldPath to (POSIX path of addedItem as string)
						-- remove the '/' from the oldPath
						if oldPath ends with "/" then set oldPath to ¬
							text 1 thru -2 of oldPath
						
						set oldItem to (addedItem as string)
						
						-- Calculate what the path SHOULD be after move (this is for the callback session)
						set proposedLocation to (myStorage & ":" & myFilename as string)
						if (addedItem as string) ends with ":" then set proposedLocation to ¬
							proposedLocation & ":"
						
						-- move to storage drive
						log_event("Storing.") of toolbox
						move addedItem to myStorage
						delay 1
						
						log_event(oldItem) of toolbox
						-- if there's a duplicate delete the duplicate
						if exists folder oldItem then
							log_event("Duplicate was made, attempting to delete.") of toolbox
							log_event("rm -rf " & oldPath) of toolbox
							do shell script "rm -rf " & quoted form of oldPath
							log_event("Deleted.") of toolbox
						end if
						
						(* 
						**************
						Commented this out since my script decided to wait for the transfer to complete to continue the script
						**************
						
					-- Start the callback session
					repeat
						log_event("Loop entered") of toolbox
						
						set currentLocation to (addedItem as string)
						set currentSize to (size of (info for addedItem)) / 1024 as integer
						
						if proposedLocation is currentLocation and mySize is currentSize then -- the file is not busy, continue your script
							log_event("Move Finished.") of toolbox
							exit repeat
						else -- the file is busy, wait one second
							log_event("Busy.") of toolbox
							delay 1
						end if
					end repeat
					*)
						
						--create symbolic link
						log_event("Making Symlink.") of toolbox
						set posixPath to POSIX path of (proposedLocation)
						if posixPath ends with "/" then set posixPath to ¬
							text 1 thru -2 of posixPath
						do shell script "ln -s " & quoted form of posixPath ¬
							& " " & quoted form of (oldPath)
						
						log_event("Done.") of toolbox
						
					on error error_string number error_number
						log_event("Error! " & error_string)
						return {error_string} & {error_number}
					end try
				end if
			end if
		end repeat
		
	end tell
	
end adding folder items to



Hi,

in Finder and System Events the file type of an symlink is “slnk”, the file type of an alias is “fapa”

The common way to treat this kind of problem is to move the added items into a subfolder (or in an other external folder) and treat them in this new location.
This way we may rename them as we want without triggering « on adding folder items » one more time.

Yvan KOENIG (VALLAURIS, France) dimanche 29 avril 2012 15:58:44

Would I then use ‘type’ instead of ‘kind’ in this case? :confused:

Wouldn’t it still count the symlink itself as an ‘addedItem’ since it’ll be added to the binded folder regardless of a subfolder or not? :S

Yes, just try it

I’m running into another problem…

When I check the file type the first time I receive an error (because the addedItem is a folder).
So I made it run the rest of the script if it errors on the symlink check so that the next time round it’ll catch the symlink.

Unfortunately it thinks the addedItem is a folder both times. So it’s ignoring the fact that it’s a symlink and going to the path it points to.

You can see this whole process via my logs

2012-04-30 00:53:06 Init 2012-04-30 00:53:06 Hackintosh SSD:Users:jwmann:Desktop:foo:Adobe Illustrator CS5: 2012-04-30 00:53:06 1 2012-04-30 00:53:06 Hackintosh SSD:Users:jwmann:Desktop:foo:Adobe Illustrator CS5: 2012-04-30 00:53:06 Finder got an error: Can't get file type of folder Adobe Illustrator CS5 of folder foo of folder Desktop of folder jwmann of folder Users of startup disk. -1728 2012-04-30 00:53:06 'Folder' 2012-04-30 00:53:07 720914 2012-04-30 00:53:07 Over 500mb... 2012-04-30 00:53:07 Storing. 2012-04-30 00:53:27 Hackintosh SSD:Users:jwmann:Desktop:foo:Adobe Illustrator CS5: 2012-04-30 00:53:27 Duplicate was made, attempting to delete. 2012-04-30 00:53:27 rm -rf /Users/jwmann/Desktop/foo/Adobe Illustrator CS5 2012-04-30 00:53:27 Deleted. 2012-04-30 00:53:27 Making Symlink. 2012-04-30 00:53:27 Done. 2012-04-30 00:53:33 Init 2012-04-30 00:53:33 Storage HDD:Applications:Adobe Illustrator CS5: 2012-04-30 00:53:33 1 2012-04-30 00:53:33 Storage HDD:Applications:Adobe Illustrator CS5: 2012-04-30 00:53:33 Finder got an error: Can't get file type of folder Adobe Illustrator CS5 of folder Applications of disk Storage HDD. -1728 2012-04-30 00:53:33 'Folder' 2012-04-30 00:53:33 720914 2012-04-30 00:53:33 Over 500mb... 2012-04-30 00:53:33 Storing. 2012-04-30 00:53:34 Storage HDD:Applications:Adobe Illustrator CS5: 2012-04-30 00:53:34 Duplicate was made, attempting to delete. 2012-04-30 00:53:34 rm -rf /Volumes/Storage HDD/Applications/Adobe Illustrator CS5 2012-04-30 00:53:36 Deleted. 2012-04-30 00:53:36 Making Symlink. 2012-04-30 00:53:36 Done.
Any ideas on what I should try next?

you can check the class of the item to find out whether it’s a folder.

By the way: As the main part of your script is within a Finder tell block, you can get name and size directly,
info for is not needed, and POSIX path is always a string, the coercion isn’t needed too

Of course, every new item must be in the auxiliary folder.

Yvan KOENIG (VALLAURIS, France) lundi 30 avril 2012 17:40:24