Hi all,
Everyone has been so helpful with previous scripting queries, so I hope someone can help with this as I am still learning how to do this.
I want to automate (in DT Pro Office) via script the renaming of several filenames by removing a portion of the filename.
Example
OW1e_L01_B65_SBaudio_BE.mp to OW1e_L01_B65.mp
The target to remove is _SBaudio_BE
Any help would be greatly appreciated! Ideally the script would ask for the portion of the filename to be removed, much like the current ‘Replace Text’ script.
Thank you for your help in advance.
You can still use theScript menu > Rename > Rename with RegEx script.
Source
Destination
Thank you @BLUEFROG, that works a treat!
Cheeky question (for ease and simplicity as I’m inclined to forget this RegEx pattern) per-chance a script I could save to automate quicker? Or is RegEx the only option for this situation?
You’re welcome.
If you use Script menu > Open Scripts Folder, you can copy and edt any of the scripts we provide.
If it’s the same source and destion you could change these two lines to be…
set sourcePattern to display name editor pTitle info "(.*)_SBaudio_BE(.*$)"
set destPattern to display name editor pTitle info "\\1\\2"
(And yes, those are double backslashes, required for AppleScript.)
Hey @BLUEFROG,
Something like this?
tell application id "DNtp"
try
set this_selection to the selection
if this_selection is {} then error "Please select some contents."
repeat
set search_string to display name editor "Remove Text" info "Enter text to find:"
if search_string is not "" then exit repeat
end repeat
set sourcePattern to display name editor pTitle info "(.*)search_string(.*$)"
set destPattern to display name editor pTitle info "\\1\\2"
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
end try
end tell
Try this…
tell application id "DNtp"
activate
try
set this_selection to the selection
if this_selection is {} then error "Please select some contents."
repeat
set search_string to display name editor "Remove Text" info "Enter text to find:"
if search_string is not "" then exit repeat
end repeat
set sourcePattern to ("(.*)" & search_string & "(.*$)")
set destPattern to "\\1\\2"
set selectedItems to selection
repeat with selectedItem in selectedItems
set itemName to name of selectedItem
set transformedName to do shell script "echo " & quoted form of itemName & " | sed -E 's/" & sourcePattern & "/" & destPattern & "/g'"
set name of selectedItem to transformedName
end repeat
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
end try
end tell
Why not use the “Replace text” script in the “Rename” part of DT3’s script menu? Or is this script not able to replace text with nothing?
In the meantime, I changed the script so that empty strings can be used as replacement. Seems simpler than using a regexp just to remove something.
-- set replacement_string to display name editor "Replace Text" default answer "" info "Enter replacement text:"
set replacement_string to display dialog "Replace Text" default answer "" buttons {"Cancel", "Replace"} default button "Replace"
if button returned of result = "Replace" then
...
set AppleScript's text item delimiters to od
end if
One line commented out, three lines added. Might not be the most elegant solution but works for me.
Hi @BLUEFROG,
Thank you for taking the time to write this script. For some reason it isn’t working for me?
I’ve saved it to the Scripts folder (along with the others), but when I run it, it returns no results.
I will keep playing with RegEX until I can figure it out.
Grateful for the help!
Just in case … as I said before, the RegExp-Stuff is far too complicated to just delete something from a name. I propose this modified Rename script that excepts an empty string as replacement (which the original doesn’t)
-- Replace Text in Names
-- Created by Christian Grunenberg Sat May 15 2004.
-- Copyright (c) 2004-2019. All rights reserved.
-- Based on (c) 2001 Apple, Inc.
tell application id "DNtp"
try
set this_selection to the selection
if this_selection is {} then error "Please select some contents."
repeat
set search_string to display name editor "Replace Text" info "Enter text to find:"
if search_string is not "" then exit repeat
end repeat
-- set replacement_string to display name editor "Replace Text" default answer "" info "Enter replacement text:"
set result to display dialog "Replace Text" default answer "" buttons {"Cancel", "Replace"} default button "Replace"
if button returned of result = "Replace" then
set replacement_string to text returned of result
set od to AppleScript's text item delimiters
repeat with this_item in this_selection
set current_name to name of this_item
if current_name contains search_string then
set AppleScript's text item delimiters to search_string
set text_item_list to every text item of current_name
set AppleScript's text item delimiters to replacement_string
set new_item_name to text_item_list as string
set the name of this_item to new_item_name
end if
end repeat
set AppleScript's text item delimiters to od
end if
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
end tell
2 Likes