I’m having trouble with my script where I’m trying to select a directory and iterate through every item in the directory in order to perform actions on them.
My initial version of this script is one that works based on ‘Folder Actions’ so I didn’t have to rely on a ‘source’ file, it was just based on whatever was ‘Added’
Now I’m converting it into a script so that I can choose my source.
For some reason I keep getting a ‘File not found’ error where the path it lists is absolutely correct and does exists. (Otherwise how would it have listed the path in the first place?)
For reference, the source I’m using is: HDD:Applications
The scripts stops right after:
log_event("Currently working on: " & addedItem) of toolbox
Here is the script:
-- Remember previous transfers in order to avoid duplicates
property historyList : {}
set toolbox to load script alias ¬
((path to library folder from user domain as string) ¬
& "Scripts:toolbox.scpt")
-- Script start
log_event("Script Init") of toolbox
-- Set Source Path
set mySource to (choose folder with prompt "Select the source folder")
-- Set Storage Path
set myStorage to "Storage HDD:Applications"
-- Begin Logic
tell application "Finder"
-- Making list of items to iterate through
set addedItems to every item of mySource
-- Count how many items added
set addedAmount to count addedItems
log_event(addedAmount & " items added.") of toolbox
-- Grab list of filenames from the Storage Path for comparison for duplicates
--set storage_list to name of every file of entire contents of myStorage
-- Start Loop
repeat with addedItem in addedItems
log_event("Currently working on: " & addedItem) of toolbox
-- Check for duplicates
if historyList contains (info for addedItem)'s name then
-- Found the filename in the history
log_event("Duplicate detected, ignoring.") of toolbox
log_event("Done.") of toolbox
else
-- No duplicates, continuing...
-- Save the size of the added item
set mySize to (size of (info for addedItem)) / 1024 as integer
log_event("File size is " & mySize & "kb") of toolbox
-- If it's too big; create an symlink
if mySize is greater than 500000 then
log_event("File Size is over threshold, Mapping...") of toolbox
try
-- Grab the filename
tell (info for addedItem) to set myFilename to name
-- Add to History variable
tell (info for addedItem) to set end of historyList 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
-- Record previous location for duplication check
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
-- if there's a duplicate or it was copied instead of moved, delete the duplicate/copy
if exists 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
--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 errMsg number errNum
log_event(errMsg & " " & errNum) of toolbox
end try
else
log_event("Doesn't reach file size threshold, ignoring...") of toolbox
log_event("Done.") of toolbox
end if
end if
end repeat
end tell
the class of addedItem is Finder file specifier, info for expects an alias.
This causes the error. But as you’re talking to the Finder anyway, use the name property directly
1st thing, it doesn’t work because they’re not alias’? how do I make them alias’? I tried doing:
-- Making list of items to iterate through
set addedItems to every item of mySource as alias
but I got a huge amount of errors for some reason.
2nd thing, How come I need (info for addedItem)'s name in my ‘Folder Action’ script (It didn’t work without it) but you suggest I remove it on this script?
Okay, i’ve done this and it seems to actually start.
I’m now getting this new error - http://d.pr/i/GaGj/1iz5RSpC
The scripts now errors right after this line:
log_event("File Size is over threshold, Mapping...") of toolbox
-- Remember previous transfers in order to avoid duplicates
property historyList : {}
set toolbox to load script alias ¬
((path to library folder from user domain as string) ¬
& "Scripts:toolbox.scpt")
-- Script start
log_event("Script Init") of toolbox
-- Set Source Path
set mySource to (choose folder with prompt "Select the source folder")
-- Set Storage Path
set myStorage to "Storage HDD:Applications"
-- Begin Logic
tell application "Finder"
-- Making list of items to iterate through
set addedItems to every item of mySource
-- Count how many items added
set addedAmount to count addedItems
log_event(addedAmount & " items added.") of toolbox
-- Grab list of filenames from the Storage Path for comparison for duplicates
--set storage_list to name of every file of entire contents of myStorage
-- Start Loop
repeat with addedItem in addedItems
log_event("Currently working on: " & addedItem) of toolbox
-- Check for duplicates
if historyList contains addedItem's name then
-- Found the filename in the history
log_event("Duplicate detected, ignoring.") of toolbox
log_event("Done.") of toolbox
else
-- No duplicates, continuing...
-- Save the size of the added item
set mySize to (size of addedItem) / 1024 as integer
log_event("File size is " & mySize & "kb") of toolbox
-- If it's too big; create an symlink
if mySize is greater than 500000 then
log_event("File Size is over threshold, Mapping...") of toolbox
try
-- Grab the filename
set myFilename to name of addedItem
-- Add to History variable
set end of historyList to name of addedItem
-- 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
-- Record previous location for duplication check
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
-- if there's a duplicate or it was copied instead of moved, delete the duplicate/copy
if exists 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
--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 errMsg number errNum
log_event(errMsg & " " & errNum) of toolbox
end try
else
log_event("Doesn't reach file size threshold, ignoring...") of toolbox
log_event("Done.") of toolbox
end if
end if
end repeat
end tell
I added a check for ‘missing value’ and it stops at the same place as before.
Perhaps I’m doing it wrong?
-- Remember previous transfers in order to avoid duplicates
property historyList : {}
set toolbox to load script alias ¬
((path to library folder from user domain as string) ¬
& "Scripts:toolbox.scpt")
-- Script start
log_event("Script Init") of toolbox
-- Set Source Path
set mySource to (choose folder with prompt "Select the source folder")
-- Set Storage Path
set myStorage to "Storage HDD:Applications"
-- Begin Logic
tell application "Finder"
-- Making list of items to iterate through
set addedItems to every item of mySource
-- Count how many items added
set addedAmount to count addedItems
log_event(addedAmount & " items added.") of toolbox
-- Grab list of filenames from the Storage Path for comparison for duplicates
--set storage_list to name of every file of entire contents of myStorage
-- Start Loop
repeat with addedItem in addedItems
log_event("Currently working on: " & addedItem) of toolbox
-- Check for duplicates
if historyList contains addedItem's name then
-- Found the filename in the history
log_event("Duplicate detected, ignoring.") of toolbox
log_event("Done.") of toolbox
else
-- No duplicates, continuing...
-- Save the size of the added item
set mySize to (size of addedItem) / 1024 as integer
log_event("File size is " & mySize & "kb") of toolbox
-- Check for missing value
if mySize is not missing value then
-- If it's too big; create an symlink
if mySize is greater than 500000 then
log_event("File Size is over threshold, Mapping...") of toolbox
try
-- Grab the filename
set myFilename to name of addedItem
-- Add to History variable
set end of historyList to name of addedItem
-- 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
-- Record previous location for duplication check
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
-- if there's a duplicate or it was copied instead of moved, delete the duplicate/copy
if exists 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
--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 errMsg number errNum
log_event(errMsg & " " & errNum) of toolbox
end try
else
log_event("Doesn't reach file size threshold, ignoring...") of toolbox
log_event("Done.") of toolbox
end if
else
log_event("Missing Size Value, skipping") of toolbox
end if
end if
end repeat
end tell
You have to check for missing value before doing math with the value
set mySize to size of addedItem
log_event("File size is " & mySize & "kb") of toolbox
-- Check for missing value
if mySize is not missing value then
set mySize to mySize / 1024 as integer
Wow I did the biggest facepalm ever with that. sigh
I changed and the script and it ran through all the items.
Unfortunately 80% of the items were skipped because of missing value…
Do you know why that would be? I’m only checking the file size…