Hi I am new to AppleScript and want to change _ to - in text edit documents. Or put another way turn underscores to dashes. Could someone advise me as to how to proceed.
This is the code I am trying but cannot seem to get to work (someText is the line of text I want the code to fix):
– find : _
– replace : -
– someText : Invincible_Yoga_Music_Singh_Kaur_Meditation_Videos.html
on replaceText(Find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
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 prevTIDs
return someText
end replaceText
tell application "TextEdit"
set teText to text of document 1
set text of document 1 to my replaceText("_", "-", teText)
end tell
on replaceText(findText, replaceText, someText)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to findText
set myItems to text items of someText
set AppleScript's text item delimiters to replaceText
set returnString to myItems as string
set AppleScript's text item delimiters to tid
return returnString
end replaceText
TextEdit has the ablility to do this sort of thing:
tell application "TextEdit" to set every character of front document where it is "_" to "-"
But it’s historically been very slow at it ” especially with long documents ” so the vanilla, text item approach might be best. Still, the technique’s worth knowing.