Trying to find and replace text in clipboard using applescript - 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.

Related

Removing a word from a variable in Applescript

I'm pretty newbie at Applescript and I can't work out how to remove a word from a variable if the word contains a “#” in it.
My script gets this error -> "Can’t make word into type integer." number -1700 from word to integer
Here's my script so far:
activate application "Grids"
delay 2
tell application "System Events"
keystroke "a" using command down
delay 0.25
keystroke "c" using command down
delay 0.25
set Description to the clipboard
if any word in Description contains "#" then delete that word
return Description
end tell
Any pointers?
Cheers,
Chris
To get text out of the clipboard, use (clipboard as text). The clipboard can contain almost anything, even multiple objects, in multiple formats, so as text gives you a string to work with.
And watch out: 'Description' appears to be part of some existing appleScript 'terminology', at least on the Mac I have right here, so I am changing your identifier to desc here:
activate application "Grids"
delay 2
tell application "System Events"
keystroke "a" using command down
delay 0.25
keystroke "c" using command down
delay 0.25
set desc to the clipboard as text
end tell
set out to {}
set tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
repeat with anItem in (text items of desc)
set str to (anItem as string)
if (str does not contain "#") then
set end of out to str
end if
end repeat
set outStr to out as string
set AppleScript's text item delimiters to tids
return outStr
This code just returns the text you are looking for. It does not re-insert the groomed string, or do anything else interesting.
I assume you're going to tell System Events to paste it via cmd-v. (Remember to set the clipboard to outStr before you paste!)
AppleScript's text item delimiters allows the string to be split and reassembled using a space (or any other token you wish). For code hygiene reasons, it's wise practice to store it before changing it, then reset it to its original value afterwards, as shown here, otherwise odd things might happen in scripts which expect it to have the default value.

Mac OS Script is supposed to paste a UNC path as SMB, but it keeps converting the colon to a semicolon and converts the capital letters to lower-case

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
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.

How to search itunes store for app with applescript?

How to search itunes store for app with applescript? I have been stuck on this and recording doesn't work in itunes.
I haven’t seen recording work in years. In this case, though, I’m not sure it could. While iTunes is scriptable, it doesn’t appear to have any direct means to perform a search of the Stores.
However, there is a means to perform a search using a URL and “open location”. It would look something like:
tell application "iTunes"
open location "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&country=US&media=software&term=ia%20Writer"
end tell
Put the text you want to search for after &term=; here, that’s ia%20Writer (with the %20 meaning a space).
This combines info from Ars Technica and Doug’s AppleScripts.
Here's a way you can use an AppleScript display dialog to enter what App to search for in the iTunes Store using the answer provided by Jerry Stratton:
If the search has spaces in it, the script replaces it with %20 for you.
set theAppSearch to ""
set theAppSearch to text returned of (display dialog "App Search iTunes Store for:" default answer "" buttons {"Cancel", "OK"} default button 2)
if theAppSearch is not "" then
set theAppSearch to my replaceSpaces(theAppSearch)
tell application "iTunes"
activate
open location "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&country=US&media=software&term=" & theAppSearch
end tell
end if
on replaceSpaces(theString)
set AppleScript's text item delimiters to {" "}
set theString to text items of theString
set AppleScript's text item delimiters to {"%20"}
set theString to theString as text
set AppleScript's text item delimiters to {""}
return theString
end replaceSpaces

Copy Concatenation in AppleScript as a command

so I have a quick question for anyone willing to help. When I am programming in a regular text editor, I often find myself wanting to copy multiple lines of code, concatenating them on the fly and then being able to paste their contents just like I would normally use Command+c/Command+v.
I think this would be done in Applescript and then assigned a custom shortcut with Quicksilver but I am not too familiar with either. Below is an example to illustrate the desired behavior.
Copy-Concatenate with Command+C+X Paste with Command+V Clear Clipboard when regular Command+C is used
so...
Command+C+X "First line" ... Command+C+x"Second line" ... Command+V pastes "First lineSecond line"
and Command+C"Third line"... Command+V pastes "Third line"
Can someone help me accomplish this or set me on the right path? Thank you in advance.
Select the text in your editor and try this:
tell application "System Events" to keystroke "x" using command down
set myText to the clipboard as «class utf8»
set {TID, text item delimiters} to {text item delimiters, {return, linefeed, character id 8233, character id 8232}}
set myText to text items of myText
set AppleScript's text item delimiters to space
set the clipboard to myText as text
set text item delimiters to TID
tell application "System Events" to keystroke "v" using command down

Unable to manipulate text from Clipboard in Applescript

I am forced to use a piece of software that requires a lot of data entry into simple web forms, so I have designed an AppleScript to take a text file as input and then keystroke out the contents into Safari, so that tabs in the text file are used to advance to the next field in the web form. I am doing some string replacement so I can treat newlines in the text file as tabs, for readability of the text file.
I want to streamline this by keystroking from the clipboard, which works, but the string replacement is not happening the way it should, and I am unable to determine why. My workaround at this point is to manually find/replace newlines with tabs before copying, but that's basically just as slow as saving the file and pointing the script at it.
tell application "Safari"
activate
end tell
set sourceString to (the clipboard) as text
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\n"
set the lineList to every text item of sourceString
set AppleScript's text item delimiters to "\t"
set keystrokeString to the lineList as string
set AppleScript's text item delimiters to ASTID
tell application "System Events"
keystroke keystrokeString as text
end tell
\t and \n will compile down to invisibles if you put this into AppleScript Editor. The string editing (middle) part works as advertised when I read from a file using:
set theFile to (choose file with prompt "Select a file to read:" of type {"txt"})
open for access theFile
set fileContents to (read theFile)
close access theFile
Any ideas why the string replacement might not be working when I get the text from the clipboard?
Using the ASCII Character function when setting the delimiters should work. A linefeed is ASCII character 10. So i.e.: set AppleScript's text item delimiters to ASCII character 10
Should work.
Tabbing through forms on a page is often error prone. You should identify each form by id and populate the form with javascript.
tell application "Safari"
set URL of document 1 to "http://www.google.com/"
delay 3
do JavaScript "document.getElementById('gbqfq').value = 'My Text'" in document 1
end tell
It appears as though linefeeds are translated into carriage returns upon copying text to the clipboard, and \n will not match them.
\r will, however, and they both compile down to invisible characters, which is a bit of an annoyance.
After a little bit of testing, it looks like when you save a text file to disk and then read it with AppleScript, you must match the line endings used in the file, LF or CR with \n or \r. However, when reading text copied to the clipboard, you must always match \r.
Edit:
Many of these characters have AppleScript keywords. tab, linefeed, and return can all be used as is. My script now looks like this:
set backspace to ASCII character 8
get the clipboard
set keystrokeString to (replacement of return by tab for the result)
set keystrokeString to (replacement of linefeed by tab for the result)
set keystrokeString to (replacement of tab by tab & backspace for the result)
tell application "System Events"
keystroke keystrokeString as text
end tell
on replacement of oldDelim by newDelim for sourceString
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to oldDelim
set strtoks to text items of sourceString
set text item delimiters of AppleScript to newDelim
set joinedString to strtoks as string
set text item delimiters of AppleScript to oldTIDs
joinedString
end replacement
It also now includes backspace characters that are hit after every tab, so if text is not entered into an already filled field, it is deleted after being advanced to.

Resources