Basically I need to search in clipboard contents for an IP address and replace it with an address that's in a completely different subnet: ( to as examples). I tried using the find and replace template in Automator but it simply wasn't working with copied text. I'm also kind of a noob with AppleScript syntax.

  • 2
    You said "I need to search for in clipboard contents and change it to as examples.", so can you please edit your question adding more details, because saying "as examples" make this a bit broad and lacking in real details. Are these IP Addresses the only substitution you're trying to make or what? How and when did the info get placed on the clipboard, is there a possibility the substitution can be made before it get to the clipboard? Is the Clipboard content formatted or plain text? Etc.! – user3439894 Apr 20 '17 at 20:12
  • Were coping a full SMB path out of Finder and then pasting it into slack. Ideally I have another service working thanks to a few users over at my other post: apple.stackexchange.com/questions/281156/… ~ but it'd be super beneficial if I were able to combine both of these scripts into one. Changing the clipboard contents from smb:// over to \\\folder and then the ol' paste command will drop the results wherever you want. – charmndr Apr 27 '17 at 17:46
  • You have not addressed all what I asked in my first comment to your question! Is to IP Address substitution the only one you need to make or are there other IP Address you need to substitute. Again I implore you to edit your question adding the appropriate details! – user3439894 Apr 27 '17 at 18:11
  • If you are wanting to do this in conjunction with the other question, (apple.stackexchange.com/questions/281156), why can't you make the substitution within the example already presented!? The on searchReplace(theText, SearchString, ReplaceString) handler is fully capable of making the substitution! After the set convertedText to searchReplace(selectedText, "smb://", "\\\\") line of code use, e.g., set convertedText to searchReplace(convertedText, "", ""). You can add that as many times for as many pairs of IP Addresses you need to substitute. – user3439894 Apr 28 '17 at 0:20
  • Ok so I'm starting to wrap my mind around AppleScript. Getting down votes but oh well. It's all starting to come together. Thanks for the elevated help @user3439894 !! – charmndr Apr 28 '17 at 19:35

You can use this shell command to check for clipboard contents and replace them:

[[ $(pbpaste) == "" ]] && printf "" | pbcopy

Basically, this checks if the current clipboard (which pbpaste outputs) is equal to the first string. If it is, it pipes the output of the printf command to pbcopy, which writes to the clipboard. You can add this to an AppleScript and have it repeat with:

    do shell script "[[ $(pbpaste) == \"\" ]] && printf \"\" | pbcopy; exit 0"
    delay 1
end repeat

(You have to doctor it up a bit with the \ and ; exit 0 so it runs in AppleScript, but this should check every second and replace the clipboard)

  • Your first part of the answer assumes that the clipboard only contains an IP Address, which may or may not be the case as it certainly isn't clear from the OP. Also, what's with the endless repeat loop triggering every second? There is absolutely nothing whatsoever in the OP looking to do an endless loop and even if there was, how are you going to break out of the endless loop!? Never post code that causes an endless loop without also providing a programatic way of breaking out of the endless loop! – user3439894 Apr 21 '17 at 1:27
  • Yes, sorry and late to the party. I should have clarified that this would be ideal as a Service menu item. Is that pretty easy to implement? I'll see what I can find with what Leo said above but it kind freaks me out regarding an endless loop LOL. – charmndr Apr 27 '17 at 16:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .