I’m trying to create a folder action that loops over all of the added files, checks them to see if they are PDFs, uses pdftk to check if they come from a certain domain and have an appended first page of a different size, and then uses pdftk to remove the first page. Note that pdftk will not copy onto the existing file, so I have to copy it to a temporary directory and then output to the original filename.
on adding folder items to this_folder after receiving added_items
repeat with theFile in added_items
-- display dialog theFile as string
set the theFileInfo to the info for theFile
if (alias of the theFileInfo is false and the file type of the theFileInfo is "pdf") or (the name extension of the theFileInfo is "pdf") then
set thePath to quoted form of POSIX path of theFile
set tempDir to path to temporary items
set tempPath to quoted form of POSIX path of (tempDir & "temp.pdf" as string)
set fromURL to do shell script "mdls -name kMDItemWhereFroms -raw " & thePath & " | grep -c URLname |cat"
if fromURL = "1" then
try
set pageOne to do shell script "/opt/local/bin/pdfinfo -f 2 -l 2 " & thePath & " | grep 'Page.*size:' | sed -E 's/Page +[0-9]+ //'"
set pageTwo to do shell script "/opt/local/bin/pdfinfo -f 1 -l 1 " & thePath & " | grep 'Page.*size:' | sed -E 's/Page +[0-9]+ //'"
if pageOne is not equal to pageTwo then
do shell script "cp " & thePath & " " & tempPath & "; /opt/local/bin/pdftk " & tempPath & " cat 2-end output " & thePath
end if
end try
end if
end if
end repeat
end adding folder items to
Here’s the part that’s driving me nuts. This script works fine if I only add one file, testing it correctly and removing the page if it passes. The script works fine if I uncomment out that display dialog and click OK through each file, looping over each file and processing accordingly. Without the dialog, the script appears to do nothing. I’m guessing this might be some kind of race condition or something, but I’m not familiar enough with applescript to understand what is going on.
edit: There was a small typo in the script from copy-pasting the wrong thing.
I don’t know for sure what’s causing the problem, but it could be that each modification to a file is causing the script to retrigger ” by fooling the Folder Actions system into thinking another file’s been added to the folder. Modifying items in a watched folder is known to cause problems. Try outputting to another folder.
You could also try sticking an ‘on error’ section in the the ‘try’ statement with a dialog to display any caught error messages.
The ‘file type’ of a PFD file is "PDF ", with a space after the “F”. But your ‘name extension’ condition will catch files with that extension.
Do you really the ‘name extension’ condition as an alternative to both the two together? Or do you want ‘alias of the theFileInfo’ to be false in both cases? Just asking. If you mean the latter, the logic in the ‘if’ line would be:
if (alias of the theFileInfo is false) and (the file type of the theFileInfo is "PDF " or the name extension of the theFileInfo is "pdf") then
I am glad I gave this some more thought for once, before answering, because an hour earlier or so, the answer would have been. “By hazel, or use Launchd”, but the truth is, is that the problem, may as well have persisted with launchd and watchpaths, or watchfolders, because of the changing of the modification date during processing. (Hazel, can probably deals with this situation correctly.)
I think Nigel nailed the problem, when he pointed . But still, you’ll have to process the files somewhere.
But why not have the folder action, send the file over to a droplet, that then will move the files from the “drop-folder” into a work folder, where the work, with changing the files will be done?
(You can do it all from the folder action too, as well, but then you steal time from the folder action with processing, and this may again cause dropped/downloaded files to “slip through” unnoticed.
The original goal was to strip off the offending page whenever I downloaded a document from a particular website which appends an annoying splash page to all its documents that screws up the viewing lines. Since it would run in the background in my downloads folder, it would actuate upon download, and as I opened it in Preview it would already have been done. Having it pipe to another folder kind of ruins that nice workflow I had. But if it’s not possible, it’s not possible.
Do folder actions have to specify multiple files? Or rather is there some way to force the script to pretend like it’s only getting one file at a time?
As a side note, when I try to debug it using the trick of commenting out
on adding folder items to this_folder after receiving added_items
and adding
set this_folder to choose folder
set added_items to choose file with multiple selections allowed
everything works just fine. My suspicion is that its an issue with the folder action itself.