Rename files - replace a string

In trying to replace a string from a filename, I go to this menu and select “Replace Text…”:

rename-file--replace-text

After I enter a string to search for, I am asked - in the next step - to enter a replacement text. But all I want is the first string to be removed, so I don’t have any replacement text. But I am not allowed to complete the operation without a replacement text:

rename--enter-replacement-text

How else can I remove partial strings from filenames?

Either by customizing the script or entering a space.

Excellent! Didn’t think of that :slight_smile:

Here is what I needed to change in that script:

-- 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
		
                -- HERE: Set the replacement_string to an empty string
		set replacement_string to "" -- display name editor "Replace Text" info "Enter replacement text:"
		
		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
		
	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

Just for the heck of it, the same thing in JavaScript. No need to change text delimiters etc.

(() => {
'use strict';
var app = Application('DEVONthink 3');
app.includeStandardAdditions = true;

var sel = app.selection();

if (sel.length === 0) {
  app.displayAlert("Bitte Datensätze auswählen");
  return;
}
try {
	var s = app.displayDialog("Suchtext", {defaultAnswer: ""});
	var searchText = s.textReturned;
	if (searchText === "") {
		return;
	}
	var r = app.displayDialog(searchText + "\nErsetzen durch", {defaultAnswer: ""});
	var replaceText = r.textReturned;

	var searchRE = new RegExp(searchText,"g");
	sel.forEach(el => {
  		var n = el.name();
  		el.name = n.replace(searchRE,replaceText);
  	});
}
catch(e) {
}
})();
2 Likes