Find and Paste Apple Script

72 views Asked by At

I have found an AppleScript below that finds and replaces... However instead of replacing with "401" in this case I want it to replace with what is on the clipboard.

So I am trying to put something like: set stringToReplace to keystroke "v" using command down...But that doesn't work (Expected end of line, etc. but found identifier.)

Here is the code:

on run {input, parameters}

    set stringToFind to "404"
    set stringToReplace to "401"
    set theFile to choose file
    set theContent to read theFile as «class utf8»
    set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, stringToFind}
    set ti to every text item of theContent
    set AppleScript's text item delimiters to stringToReplace
    set newContent to ti as string
    set AppleScript's text item delimiters to oldTID
    try
        set fd to open for access theFile with write permission
        set eof of fd to 0
        write newContent to fd as «class utf8»
        close access fd
    on error
        close access theFile
    end try

    return input
end run
1

There are 1 answers

0
tbowden On BEST ANSWER

Solved the issue:

All I needed to do was add: the clipboard after set stringToReplace to

on run {input, parameters}

    set stringToFind to "404"
    set stringToReplace to the clipboard
    set theFile to choose file
    set theContent to read theFile as «class utf8»
    set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, stringToFind}
    set ti to every text item of theContent
    set AppleScript's text item delimiters to stringToReplace
    set newContent to ti as string
    set AppleScript's text item delimiters to oldTID
    try
        set fd to open for access theFile with write permission
        set eof of fd to 0
        write newContent to fd as «class utf8»
        close access fd
    on error
        close access theFile
    end try

    return input
end run