Apple Script put clipboard list in quotes - applescript

Does anyone know of an applescript that will take whatever is on the clipboard and wrap it in quotes. (Ideally triggered by key shortcut, but i can set that up myself)
Example (Copied Data from excel col)
Beer
Whisky
Chronic
Would turn into
'Beer','Whisky','Chronic'
I can't find this anywhere and I can only do some basic stuff in Applescript but I totally think it would help a lot of devs/db admins!

Assuming the source is plain text and the items are separated by newline characters
you can do it with text item delimiters
set theText to "Beer
Whisky
Chronic"
set {TID, text item delimiters} to {text item delimiters, {linefeed, return}}
set theList to text items of theText
set text item delimiters to "','"
set theResult to "'" & (theList as text) & "'"
set text item delimiters to TID
theResult
To get the data directly from the clipboard replace the first line with
set theText to the clipboard

thanks to #vadian for the help there - the final working script
takes input from clipboard, transforms and then puts back to clipboard
set theText to (do shell script "pbpaste")
set {TID, text item delimiters} to {text item delimiters, {linefeed, return}}
set theList to text items of theText
set text item delimiters to "','"
set theResult to "'" & (theList as text) & "'"
set text item delimiters to TID
set the clipboard to theResult
theResult
I've set it up as a service using automator and assigned a keyboard shortcut for quick transform.

Related

Extract specific row from clipboard with AppleScript

I'm using the following code to get the content of my clipboard into my Automator code:
set theString to get the clipboard
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
However, if the user has copied the following lines:
word1;word2;word3
word4;word5;word6
How do I extract the second row only? I'm not interested in the first row, and the number of columns can change.
The following example AppleScript code works for me:
set myTextItemDelimiter to ";"
if (clipboard info for string) is {} then
display dialog ¬
"The clipboard does not contain text." buttons {"OK"} ¬
default button 1 giving up after 5
return
end if
set theString to (the clipboard) as string
if theString does not contain myTextItemDelimiter then
display dialog ¬
"The clipboard does not contain the \"" & ¬
myTextItemDelimiter & "\" delimiter." buttons {"OK"} ¬
default button 1 giving up after 5
return
end if
if (count paragraphs of theString) is greater than 1 then
set theString to paragraph 2 of theString
else
display dialog ¬
"The clipboard only contains one line of text." buttons {"OK"} ¬
default button 1 giving up after 5
return
end if
set {TID, text item delimiters} to {text item delimiters, myTextItemDelimiter}
set theList to text items of theString
set text item delimiters to TID
return theList
Notes:
I added some error handling to the code.
Try this
set the clipboard to "word1;word2;word3
word4;word5;word6"
set theClipboard to (the clipboard)
set secondLine to (paragraph 2 of theClipboard)
set theString to secondLine
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
OR
set the clipboard to "word1;word2;word3
word4;word5;word6"
set secondLine to paragraph 2 of (the clipboard)
set theString to secondLine
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID
OR
set the clipboard to "word1;word2;word3
word4;word5;word6"
set theString to paragraph 2 of (the clipboard)
set {TID, text item delimiters} to {text item delimiters, ";"}
set theList to text items of theString
set text item delimiters to TID

How to Send the Output of a Dialog Box to an email adress in applescript

I would like to be able to send the output of a dialog box to my email. What would be the best way to do such a thing?
It would be something sort of like this:
repeat
display dialog "Enter some text:" buttons {"Git Goin"} default answer ""
set theNewInfo to text returned of result
if theNewInfo ≠ "" then exit repeat
end repeat
Its a really simple script for a proof of concept, but what I want is as follows: When they enter any text into the dialog box, for that text to be sent to my email, regardless of what it contains.The Subject would say "NewInfo" and the body would contain the text entered into the dialog box
You should post what code you have... too many questions still to be able to answer you reliably. What are you wanting to send to the email message? email addy? subject? body? etc.
Basically, you capture the result of the dialog and then put it into a "mailto:" URL string and then use 'open location' on the URL. This should be enough to get you started:
set dialogResult to display dialog "Enter the subject" default answer "My subject" buttons {"me#example.com", "you#example.com"} default button 1
set theAddress to button returned of dialogResult
set theSubject to text returned of dialogResult
set theBody to "This%20is%20the%20body%20text:%0AMore%20text"
-- Must encode entities, spaces as %20 and line breaks as %0A (%0D%0A), etc.
set theSubject to findReplace(" ", "%20", theSubject)
set theSubject to findReplace(return, "%0A", theSubject)
set theURL to "mailto:" & theAddress & "?subject=" & theSubject & "&body=" & theBody
open location theURL
on findReplace(f, r, s)
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to f
set s to text items of s
set AppleScript's text item delimiters to r
set s to s as string
set AppleScript's text item delimiters to otid
return s
end findReplace

applescript to open google search query in youtube

I am using the code below to open youtube with google query in applescript as the new google does not carry my search string to youtube.Unable to open url in firefix with the google search string in firefox. I really appreciate any help.Thanks in advance.
I am getting an error here :
tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
set tr to get the clipboard
end tell
return trimgoogle(tr)
on trimgoogle(sourceAddress)
set AppleScript's text item delimiters to {"#"}
set addressParts to (every text item in sourceAddress) as list
set AppleScript's text item delimiters to ""
set nameOnly to item 2 of addressParts
set AppleScript's text item delimiters to {"&"}
set addressParts2 to (every text item in nameOnly) as list
set AppleScript's text item delimiters to ""
set nameOnly1 to item 1 of addressParts2
set nameOnly2 to nameOnly1
set AppleScript's text item delimiters to {"="}
set addressParts21 to (every text item in nameOnly2) as list
set AppleScript's text item delimiters to ""
set nameOnly12 to item 2 of addressParts21
set newurl to "http://www.youtube.com/results?search_query=" & nameOnly12
return newurl
end trimgoogle
tell application "Firefox"
OpenURL newurl
end tell
Try this:
tell application "Firefox"
open location newurl
end tell
EDIT:
Firefox does not activate itself automatically, so I just tried this:
tell application "Firefox"
activate
open location "http://www.wikipedia.com/"
end tell
which works here.
Here more about encoding text:
http://www.macosxautomation.com/applescript/sbrt/sbrt-08.html
EDIT2:
OK, I see what you mean, that would be solved like this:
tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
set tr to get the clipboard
end tell
set newurl to my trimgoogle(tr)
tell application "Firefox"
activate
open location newurl
end tell
on trimgoogle(sourceAddress)
set AppleScript's text item delimiters to {"#"}
set addressParts to (every text item in sourceAddress) as list
set AppleScript's text item delimiters to ""
set nameOnly to item 2 of addressParts
set AppleScript's text item delimiters to {"&"}
set addressParts2 to (every text item in nameOnly) as list
set AppleScript's text item delimiters to ""
set nameOnly1 to item 1 of addressParts2
set nameOnly2 to nameOnly1
set AppleScript's text item delimiters to {"="}
set addressParts21 to (every text item in nameOnly2) as list
set AppleScript's text item delimiters to ""
set nameOnly12 to item 2 of addressParts21
set newurl to "http://www.youtube.com/results?search_query=" & nameOnly12
return newurl
end trimgoogle

Issue with applescript function

I'm trying to the function replaceChars in the below script but getting the following error:
error "Finder got an error: Can’t continue replace_chars." number -1708
The intention is to add the script to finder as a button so that I can simply click it to copy the path to my clipboard. I'm adding file://localhost/ so that the link can then be used when shared with users by email as a direct link to a folder on the local network. If possible I would also like to add to clipboard the same for Windows machines.
If you could offer any guidance as to the task above it would be much appreciated, this is my first attempt at programming with applescript so I'm not that knowledgable of how things are done.
Heres the code:
on appIsRunning(appName)
tell application "System Events"
set isRunning to ((application processes whose (name is equal to appName)) count)
end tell
if isRunning is greater than 0 then
return true
else
return false
end if
end appIsRunning
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
if appIsRunning("Finder") then
tell application "Finder"
set thePath to "file://localhost/" as text
set theTarget to (target of front Finder window) as text
set the clipboard to thePath & replace_chars(theTarget, ":", "/") as text
end tell
end if
AppleScript is looking for a replace_chars handler in the Finder's scripting dictionary. You can either make it into my replace_chars to have AS look in the script, or (probably better) move the set the clipboard to thePath & replace_chars(theTarget, ":", "/") as text line out of the tell block altogether.
You can make it into a one-line script:
tell application "Finder" to set the clipboard to "file://localhost" & (target of front Finder window as text)'s POSIX path
Replace your replace_chars(theTarget, ":", "/") call with replace_chars(theTarget, ":", "/") of me:
set the clipboard to thePath & replace_chars(theTarget, ":", "/") of me as text

Remove quotes on "quoted form of" in Applescript?

I have the following code to copy paths of multiple selected items in Finder:
activate application "Finder"
tell application "Finder"
set theSel to (selection of application "Finder")
set pathList to {}
repeat with anItem in theSel
set the end of pathList to quoted form of (POSIX path of (anItem as alias))
end repeat
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
"
set the clipboard to pathList as string
set AppleScript's text item delimiters to savedDelimiters
end tell
The only problem is that it results in this:
'/Applications/Burn.app/'
'/Applications/Caffeine.app/'
'/Applications/Calculator.app/'
Thats' basically what I want, but I don't want those damn singlue quotes in there. How do I get rid of them? I already tried just removing quoted form of but without luck.
Thanks!
all you have to do is take out "quoted form of "
activate application "Finder"
tell application "Finder"
set theSel to (selection of application "Finder")
set pathList to {}
repeat with anItem in theSel
set the end of pathList to (POSIX path of (anItem as alias))
end repeat
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
"
set the clipboard to pathList as string
set AppleScript's text item delimiters to savedDelimiters
end tell

Resources