简体   繁体   中英

Trying to find and replace text in clipboard using applescript

I am trying to copy a file path to the clipboard and replace the word "Volumes" with "MyServer". Currently I can get the path and replace the spaces, which is working well. Now I just need to replace that word "Volumes" and I'm having no luck. Here is the code I currently have. Any help would be great.

tell application "Finder"
  set sel to the selection as text
  set TempTID to AppleScript's text item delimiters
  set AppleScript's text item delimiters to space
  set sel to text items of sel
  set AppleScript's text item delimiters to "%20"
  set sel to sel as string
  set AppleScript's text item delimiters to TempTID
  set the clipboard to "afp://" & POSIX path of sel

end tell

OS X Mavericks (10.9.4)

If you're only interested in changing "/Volumes/" at the head of the path, you could do something like this (this leaves the path alone if it doesn't fit the criterion):

tell application "Finder"
    set sel to the selection as text
    set TempTID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to space
    set sel to text items of sel
    set AppleScript's text item delimiters to "%20"
    set sel to sel as string
    set AppleScript's text item delimiters to TempTID
    set posixSel to POSIX path of sel
    if posixSel starts with "/Volumes/" then
        set posixSel to ("/MyServer" & (text 9 thru end of posixSel))
    end if
    set the clipboard to "afp:/" & posixSel
end tell
--I changed to afp:/ instead of afp:// because I think you need afp:// not afp:///

If you tell a text-editing app like TextWrangler (free on the Mac App Store) to do the work instead of Finder, you can write a simpler, easier-to-maintain script:

tell application "TextWrangler"
    set theClipboardContents to the clipboard as text
    set theNewClipboardContents to replace "Volumes" using "MyServer" searchingString theClipboardContents
    set the clipboard to theNewClipboardContents
end tell

The above script also works on BBEdit, the big brother of TextWrangler.

Ideally, as you write each part of your script, you would target the most appropriate app for that functionality. The functionality is within the apps, not within AppleScript, which is a “little language” with almost no built-in functionality. So, in the same way that you would open a text editor to edit text, your scripts should tell a text editor to do the heavy lifting when they want to edit text. Similarly, your scripts should tell Finder to do the heavy lifting when they want to work with disks, folders, and files.

Also, avoiding AppleScript's text item delimiters can add years to your life.

You can create the file path from any file or folder by using AppleScript's “choose file” or “choose folder” commands, which prompt the user with dialog box that enables them to choose a file or folder, respectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM