Hi. I have 700 or so pdf+text records in a databse that I need to rename. I have manually renamed some of them - simply as “page 176”, “page 177” etc but it would be much better if I could use some automation. The records typically are named “Screenshot 2024-08-25 at 2.52.13 PM” etc. I have unsuccessfully tried the Rename with RegEx script menu item. I tried creating a smart rule with an applescript but it did nothing, It is supposed to work on a selection of records, initialise a page number, then form a new record name as ‘page’ & pagenr, then increment the page number for the next item in the selection.
see below…
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on run
tell application id "DNtp"
if selection = {} then return
set response to display dialog "Choose starting page nr" default answer "1" with icon note buttons {"Cancel", "Continue"} default button "Continue"
--> {button returned:"Continue", pagenr:"1"}
set pagenr to text returned of response
my performSmartRule(selected records)
end tell
end run
on performSmartRule(theRecords)
tell application id "DNtp"
repeat with theRecord in theRecords
set newName to {"page " & pagenr} as string -- Concatenate the "page" with the page nr
set name of theRecord to newName -- Apply the new name
set pagenr to pagenr + 1
end repeat
end tell
end performSmartRule
Has anyone got any suggestions to achieve this batch renaming job please?
Try the “Tools/Batch processing…” menu and the “Change name” process there.
Unrelated: Don’t use run
and performsmartrule
in the same script. Define your variables (pagenr
is not defined in your performsmartrule
handler, and that will not be executed by a smart rule).
Actually, this is a very smart way to test smart rule scripting as well as make a script more modular. It can be run from DEVONthink’s Scripts menu and aliased in the Smart Rules script directory for use in smart rules and batch processing.
my performSmartRule(selected records)
Agreed, this should be passing and using the parameter when calling it from the on run
section, e.g.,…
my performSmartRule(selected records, pagenr)
…
on performSmartRule(theRecords, pagenr)
use AppleScript version “2.4” – Yosemite (10.10) or later
use scripting additions
However, I wouldn’t include this in the code. (Clearly created in Script Debugger.)
@peteread: It’s unclear how you’re coming up with your naming convention of page 176, page 177, etc. And your script would force you to choose the number for each of 700 documents. That sounds only slight less cumbersome than manually renaming them.
3 Likes
Thanks for the feedback. I modified the code to the arguments of ‘performSmartRule’ and removed the use statements. It works in debug mode in Script Debugger, on a test database with a few records.
The naming convention is simply the page number of the pdf, and in this case starts at x and finishes at y, incrementing by 1. It would be nice if I could reliably identify the page number within each pdf (which is a pdf+text OCR’d from a screenshot) and use that, but that is beyond me at present. I have yet to get the script to work when invoked inside DT3, either by a smart rule, or batch process calling the script.
And thank you so much for your help. Here is the corrected code, saved in the ~/Library/Application Scripts/com.devon-technologies.think3/Smart Rules
folder.
on run
tell application id "DNtp"
if selection = {} then return
set response to display dialog "Choose starting page nr" default answer "1" buttons {"Cancel", "Continue"} default button "Continue"
--> {button returned:"Continue", pagenr:"1"}
set pagenr to text returned of response
my performSmartRule(selected records, pagenr)
end tell
end run
on performSmartRule(theRecords, pagenr)
tell application id "DNtp"
repeat with theRecord in theRecords
set newName to {"page " & pagenr} as string -- Concatenate the "page" with the page nr
set name of theRecord to newName -- Apply the new name
set pagenr to pagenr + 1
end repeat
end tell
end performSmartRule
You’re welcome!
Note, the syntax with the two parameters was intended for use as a standalone script. But to avoid confusion, here is a proper modifed smart rule only version of your script with a few small improvements…
on performSmartRule(theRecords)
tell application id "DNtp"
set {theButton, pagenr} to {button returned, text returned} of (display dialog "Choose starting page nr" default answer "1" buttons {"Cancel", "Continue"} default button "Continue")
--> {button returned:"Continue", pagenr:"1"}
if theButton is not "Cancel" then
repeat with theRecord in theRecords
set newName to {"page " & pagenr} as string -- Concatenate the "page" with the page nr
set name of theRecord to newName -- Apply the new name
set pagenr to pagenr + 1
end repeat
end if
end tell
end performSmartRule
Also, we generally discourage user interaction like dialogs in smart rules. While not a hard limitation, rules should just operate transparently in the background. However, it could be useful in a Tools > Batch Process.
If you wanted to run this as a standalone script, here is the code modified code…
on run
tell application id "DNtp"
if selection is not {} then my performSmartRule(selected records)
end tell
end run
on performSmartRule(theRecords)
tell application id "DNtp"
set {theButton, pagenr} to {button returned, text returned} of (display dialog "Choose starting page nr" default answer "1" buttons {"Cancel", "Continue"} default button "Continue")
--> {button returned:"Continue", pagenr:"1"}
if theButton is not "Cancel" then
repeat with theRecord in theRecords
set newName to {"page " & pagenr} as string -- Concatenate the "page" with the page nr
set name of theRecord to newName -- Apply the new name
set pagenr to pagenr + 1
end repeat
end if
end tell
end performSmartRule
If you saved this in the ~/Library/Application Scripts/com.devon-technologies.think3/Menu
directory, you could run it on demand from our Scripts menu. If you then aliased the script into the Smart Rules
directory, you could use it as an external script for the Apply Script
action in smart rules and batch processing.
Sorry for any confusion.
PS: Detecting page folios (which are not the same as page numbers) likely would be very difficult expect in unusual circumstances and not broadly applicable over many different documents.
Thanks very much. I can see why you made the modifications and the script now works inside DT3 and has saved me hours of work.
Hope it’s useful to other members.
Regards
Pete
1 Like