My workflow: I use a 13" e-ink eReader to read my pdfs and I want them organized in the same folders as in my Bookends library. I don't want to do this manually.
What the script does: The shell script monitors the attachment folder and it's subfolders for new files or renamed files. If such a file is detected and it's name is of the appropriate format (see below) it is moved in a folder recreating the folder structure of the bookends library
Requirements: Install Mac ports and then fswatch and terminal-notifier. Instruct Bookends to rename files using a format that begins with g; (group followed by semicolon - it is removed when the script runs). Tick the only ASCII names box.
Procedure:
- 1. Create an automator application with the shell script as its single action.
2. Modify the SCR_PATH and ATT_PATH variables to point to the attachment folder and the Applescript file respectively
3. Include the automator app in the login items to start the monitoring automatically after logging in.
Limitations:
1. Bookends first adds the Pdf to the attachment folder and then renames it according to the chosen format. This will trigger a notification from the script that the pdf is not of the correct format but then pdf will be correclty moved .
2. Pdfs that belong to multiple groups are not processed; I will try to address this issue when I find some time.
Code: Select all
ATT_PATH=/path/to/attachments/folder;
SCR_PATH=/path/to/Applescript;
/opt/local/bin/fswatch -0r -e ".*" -i "\\.pdf$" --event Created --event Renamed "$ATT_PATH" | xargs -0 -n1 -I{} -P2 sh -c '[ -f "$0" ]&&[ ! -f "${2}/.locked" ]&&returnVal=$(osascript "$1" "$0" "$2")&&/opt/local/bin/terminal-notifier -title "${returnVal%% *}" -message "${returnVal#* }"' {} "$SCR_PATH" "$ATT_PATH"
Code: Select all
on run args
try
-- this Applescript renames and moves files that are added to Bookends attachment folder (or are renamed by the application)
-- the following line checks wheteher Bookends is running as the PDF could have been renamed/added by the user
if application "Bookends" is not running then return "Fail Hellooo! Bookends is not running"
set thePDF to item 1 of args
set attachmentsPath to item 2 of args
set lockFilePath to attachmentsPath & "/.locked"
if LockIsOn(lockFilePath) is true then return
set Creturn to ASCII character 13
set Slash to ASCII character 47
set Semicolon to ASCII character 59
set oldDelims to AppleScript's text item delimiters
-- extract the name of the pdf and the static group it belongs to
tell application "System Events" to set fullFileName to name of (POSIX file thePDF as alias)
set AppleScript's text item delimiters to Semicolon
set fileNameComponents to every text item in fullFileName
if (count of fileNameComponents) is 1 then
return "Fail No ';' found in filename"
end if
set activeGroup to item 1 of fileNameComponents
set fileName to item 2 of fileNameComponents
-- escape parentheses in attachment name for SQL regular expression to work
set fileNameRegExp to replaceText("(", "\\(", fileName)
set fileNameRegExp to replaceText(")", "\\)", fileNameRegExp)
set fileNameRegExp to quoted form of fileNameRegExp
-- find if a reference exists with the given attachment name and get the id
set RegExp to "attachments REGEX " & fileNameRegExp
tell application "Bookends" to set RefNo to «event ToySSQLS» RegExp
if RefNo is "" then
return "Fail No attachment with following name found: " & fileName
end if
-- search for the path of the group the pdf belongs to in the group hierarchy
tell application "Bookends" to set groupHierarchy to return «event ToySRGPN» given «class PATH»:"true"
set AppleScript's text item delimiters to Creturn
set delimitedGroupHierarchy to every text item of groupHierarchy
set AppleScript's text item delimiters to Slash
repeat with singleGroupPath in delimitedGroupHierarchy
set singleGroup to last text item of singleGroupPath
if singleGroup is equal to activeGroup then
-- when found create the path in the attachments folder, move and rename the pdf
set quotedDestinationFolder to quoted form of (attachmentsPath & "/" & singleGroupPath)
set quotedFullFileName to quoted form of thePDF
set quotedDestinationFullFileName to quoted form of (attachmentsPath & "/" & singleGroupPath & "/" & fileName)
set destinationFolder to attachmentsPath & "/" & singleGroupPath
set cmdStr to "touch " & quoted form of lockFilePath & ";
if [[ ! -d " & quotedDestinationFolder & " ]];
then mkdir -p " & quotedDestinationFolder & "; fi;
mv " & quotedFullFileName & " " & quotedDestinationFullFileName & ";
sleep 1;
rm " & quoted form of lockFilePath
do shell script cmdStr
tell application "Bookends" to «event ToySSFLD» RefNo given «class FLDN»:"attachments", string:fileName
return "Success " & fileName & " was moved"
end if
end repeat
return "Fail " & fileName & " was not found in any of the groups"
on error errorMessage number errorNumber
return "ApplescriptError " & errorNumber & " : " & errorMessage
end try
end run
on LockIsOn(lockFilePath)
tell application "System Events"
if exists file lockFilePath then return true
return false
end tell
end LockIsOn
on replaceText(find, replace, someText)
set localOldDelims to AppleScript's text item delimiters
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to localOldDelims
return someText
end replaceText