Enhance the Replace Text in Filename script that Apple provides with the OS in such a way that I can
1 - select a filename, as if for editing, in a folder
2 - select part of a filename (the part I want to change) 3 - make that selection available to the script
4 - use it as the default text for “Enter text to replace in filename”.
the crucial step is #3.
I posted this question on Apple.com > Support > Discussions > Mac OS X Technologies > AppleScript and got this solution (edited):
tell application "Finder"
set curFile to item 1 of (get selection) -- get the selected file
set curFilename to name of curFile -- find its name
tell application "System Events"
tell process "Finder"
keystroke "c" using {command down} -- my step #3
end tell
end tell
end tell
I ended up using
tell application "Finder"
activate
tell application "System Events"
tell process "Finder"
keystroke "c" using {command down}
end tell
end tell
end tell
The responder (“Camelot”) states it works for him, it does not work for me because the script does not copy the selection out of the text field. It seems that the focus is lost from within the text field and moves onto the file (as alias?) by the time SystemEvents reaches it and what gets copied is the file itself, not even the filename, certainly not the selection. (And only if I put an “activate” command after “tell Finder”, without that nothing gets copied; I’m not sure what the purpose of the “set curFile” statements was).
I have UI scripting enabled.
Perhaps there is a way to directly access the selected text (it shows up as AXSelectedText(W) in UI Element Inspector) but how to do it is beyond my knowledge at this point.
tell application "Finder"
activate
set curFile to item 1 of (get selection) -- get the selected file
set curFilename to name of curFile -- find its name
end tell
tell application "System Events"
tell process "Finder"
try
set selText to value of attribute "AXSelectedText" of text field 1
on error
set selText to ""
end try
end tell
display dialog "" default answer selText buttons {"Cancel", "OK"} default button 2
set newText to text returned of result
end tell
set {TID, text item delimiters} to {text item delimiters, selText}
set curFilename to text items of curFilename
set text item delimiters to newText
set curFilename to curFilename as text
set text item delimiters to TID
tell application "Finder" to display dialog curFilename
Unfortunately it does not work either. The problem appears to be the same - by the time the script interacts withe the window, the selection is lost from the text field.
Maybe there is a system level setting that would permit a selection to remain active in text fields in windows even if some process is also active?
tell application "Finder"
set curFile to item 1 of (get selection) -- get the selected file
set curFilename to name of curFile -- find its name
end tell
display dialog "Enter new file name." default answer curFilename
I have a folder with a bunch of files that begin with IMG.BAD_####
I want to change them to IMG.GOOD_####
I fire up the stock Replace Text in Item Names script that comes with the OS. I get a blank window into which I have to type in “BAD” as the string to change then in the next window “GOOD” as the replacement string.
I am lazy. I want to select the portion of the filename that is to-be-replaced - in the Finder window itself, in the text field of the file, just like when you change a part of the name of a file and you highlight that part as if in a word processor.
I want to change every instance of BAD to GOOD. I don’t want to type it into a window that pops up seconds later. Maybe I am senile and forget what I want to change. Maybe the to-be-changed string is too long and hard to spell. Maybe I’m just a spoiled brat, an unrepenting sybarite who has nothing better to do with his time than to dabble in frivolous Applescript projects. Be there as it may, I want to highlight BAD in one of the filenames and then I want my script to use that selection and I don’t want to have to type it into a box.
I can do everything else but that step so if you respond, you don’t have to waste your time with anything after that step.
So my pseudocode is this:
Script, start running
without the Finder losing focus on the current selection in a text field in its currently active window
Access the currently highlighted text segment in the currently active Finder window
Set some value to the highlighted portion of the filename (=the value of the AXSelectedText attribute, as per UI Element Inspector)
Run the script from the Script Menu, then it works
Alternatively, if the find and replace pattern is always the same, use something like this
set theFolder to choose folder
tell application "Finder"
set findString to text returned of (display dialog "Enter find string" default answer "BAD" buttons {"Cancel", "OK"} default button 2)
set replaceString to text returned of (display dialog "Enter find string" default answer "GOOD" buttons {"Cancel", "OK"} default button 2)
repeat with oneFile in (get files of theFolder)
set theName to name of oneFile
if theName contains findString then
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, findString}
set theName to text items of theName
set AppleScript's text item delimiters to replaceString
set theName to theName as text
set AppleScript's text item delimiters to ASTID
set name of contents of oneFile to theName
end if
end repeat
end tell
That’s where it is and it does not work on my machine.
I took the liberty to change
on error
set selText to ""
to
on error
set selText to "no good"
I always get “no good”.
The text to change is always different.
So thanks for your replies but so far I’m stuck. I’m wondering if there is a changeable system setting that determines when a selection in a text field in a window loses focus. The loss of the focus is the main issue, IMHO.