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