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
Related
How to get the filename without the extension in AppleScript? For example:
Penny.Dreadful.S03E01.720p.HDTV.x264-BATV.mp4 to
Penny.Dreadful.S03E01.720p.HDTV.x264-BATV
One file to work enough for me but could be in for more file.
The following code takes the file name but with the extension. I don't want the extension. Thanks for your help.
try
set theNames to {}
tell application "Finder"
repeat with i in (get selection)
set end of theNames to name of i
end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to theNames as text
set text item delimiters to TID
end try
Try this, instead of the try block the script checks for empty selection and the fileName is extracted by subtracting the extension if there is one.
set theNames to {}
tell application "Finder"
set theSelection to selection
if theSelection is {} then return
repeat with anItem in theSelection
set {name:fileName, name extension:fileExtension} to anItem
set end of theNames to my removeExtension(fileName, fileExtension)
end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to theNames as text
set text item delimiters to TID
on removeExtension(Nm, Ex)
if Ex is missing value or Ex is "" then return Nm
return text 1 thru ((count Nm) - (count Ex) - 1) of Nm
end removeExtension
If I select a file in the finder on my local or a remote server, I want the script to paste the POSIX path to the clipboard as text, for pasting in a chat window, etc.
This is what I have so far but it is not very clean:
on run
tell application "Finder"
copy selection to theSelected
set outputPathList to {}
set anItem to theSelected
copy (POSIX path of (anItem as alias)) to end of outputPathList
set AppleScript's text item delimiters to return
set outputString to outputPathList as string
set AppleScript's text item delimiters to ""
set the clipboard to outputString
end tell
end run
Any ideas on how to clean this up?
on run
tell application "Finder"
set theItem to selection as string
end tell
set posixForm to POSIX path of theItem
set the clipboard to posixForm
end run
The property selection of the Finder returns a list of Finder specifiers.
This script copies all POSIX paths (one per line) to the clipboard.
tell application "Finder" to set theSelection to (get selection)
if theSelection is {} then return
set outputPathList to {}
repeat with anItem in theSelection
set end of outputPathList to POSIX path of (anItem as text)
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to outputPathList as text
set text item delimiters to TID
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.
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
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