Reputation: 1
I have a Script that was supposed to convert Windows UNC path to Mac SMB, but keep converting the colon to a semicolon and Upper case to lower case. Here is code. Help is appreciated.
set myClip to the clipboard
set mytext to searchReplace(myClip, "<", "")
set mytext to searchReplace(mytext, ">.", "")
set mytext to searchReplace(mytext, ">", "")
set findIt to "\\"
set replaceIt to "/"
set mylocation to searchReplace(mytext, findIt, replaceIt)
set mylocation to "smb:" & mylocation
set the clipboard to mylocation
do shell script "pbpaste |textutil -convert txt -stdin -stdout -encoding 30 |pbcopy"
tell application "System Events" to keystroke (the clipboard)
on searchReplace(theText, SearchString, ReplaceString)
set OldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to SearchString
set newText to text items of theText
set AppleScript's text item delimiters to ReplaceString
set newText to newText as text
set AppleScript's text item delimiters to OldDelims
return newText
end searchReplace
Upvotes: 0
Views: 106
Reputation: 90551
This line:
tell application "System Events" to keystroke (the clipboard)
simulates key presses. The characters in the string are converted to the corresponding key and "pressed" without modifiers like Shift (by default). You can specify a using <modifiers>
phrase on the command to specify which modifiers to use, but then those are used for all of the simulated key presses. You can't cause it to simulate the Shift for some key presses (for the upper-case letters and colon) and not others based on the characters in the string.
This seems like a good candidate for a Service rather than a stand-alone script. A service will automatically receive the selected text in the active app and its output will automatically replace that selected text, without having to use the clipboard or simulating key presses. Use Automator.app to create a service, enable "Output replaces selected text", add the Run AppleScript action, and put the guts of your script into the handler.
Upvotes: 1