Applescript Paste and Tab From a Comma Delimited List - macos

I'm trying to get an automator applescript to loop though a comma delimited list; and in doing so, paste value 1, tab, paste value 2, tab etc...
It doesn't seem to want to paste into a text field in google chrome however.
display dialog "What is the list? (Artist, Song Title, Artist, Song Title)" default answer "Frank Sinatra, My Way, Elvis, Blue Christmas"
set user_input to text returned of result
set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {","}}
set myList to text items of user_input -- Gives list {"2", "69", "12"}
set AppleScript's text item delimiters to myTID -- It's considered good practice to return the TID's to their original state
repeat with myItem in myList -- Loop through the items in the list
tell application "System Events"
set the clipboard to myItem
keystroke "v" using {command down}
keystroke tab
end tell
delay 1
end repeat
display dialog "Job Done"
return

Your paste command is done on your script, not on other application (Chrome in your case). you must tell which process should receive the keystroke. Something like :
tell application "System Events"
tell process "Chrome"
Set the clipboard to myItem
keystroke "v" using {command down}
keystroke tab
end tell
end tell

Related

AppleScript: How to extract numbers from a string?

I am writing a script to go to the NYT website on Corona, get the US data, extract numbers (total, death), and to send me a notification. I am close, but when I extract numbers and display them, they are put together (ie 700021 instead of 7000,21). My question is:
How do I extract the numbers so that they are delineated?
Here is the code:
set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show&region=TOP_BANNER&context=storyline_menu"
tell application "Safari" to make new document with properties {URL:theURL}
tell application "System Events"
repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")
delay 0.5
end repeat
end tell
to getInputByClass(theClass, num)
tell application "Safari"
set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerText;" in document 1
end tell
return input
end getInputByClass
set myVar to getInputByClass("g-body ", 5)
on returnNumbersInString(inputString)
set s to quoted form of inputString
do shell script "sed s/[a-zA-Z\\']//g <<< " & s
set dx to the result
set numlist to {}
repeat with i from 1 to count of words in dx
set this_item to word i of dx
try
set this_item to this_item as number
set the end of numlist to this_item
end try
end repeat
return numlist
end returnNumbersInString
set theNums to returnNumbersInString(myVar) as text
display notification "COVID-19 UPDATE" subtitle theNums sound name "glass"
tell application "Safari"
close its front window
end tell
You are getting a list of numbers from the returnNumbersInString handler, but just coercing the list to text doesn't normally provide any kind of formatting. One solution would be to use text item delimiters to specify the text to use when joining the list items. For example, when converting to text for the notification you could do something like:
set tempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set theNums to returnNumbersInString(myVar) as text
set AppleScript's text item delimiters to tempTID
Similar to your other question I helped you with, the target data is already in a table and as such I'd use the table data to get the information as its structure layout is not likely to change where target 'g-body ' of 5 may not always be the United States.
I get my data a little different way:
set theURL to "https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html?action=click&pgtype=Article&state=default&module=styln-coronavirus&variant=show&region=TOP_BANNER&context=storyline_menu"
tell application "Safari" to make new document with properties {URL:theURL}
tell application "System Events"
repeat until exists ¬
(UI elements of groups of toolbar 1 of window 1 of ¬
application process "Safari" whose name = "Reload this page")
delay 0.5
end repeat
end tell
tell application "Safari" to tell document 1 to set CountriesTable to ¬
do JavaScript "document.getElementsByClassName('svelte-f9sygj')[0].innerText;"
tell application "Safari" to close its front window
set awkCommand to ¬
"awk '/United States/{print $3,\"Cases &\",$4,\"Deaths\"}'"
set notificationMessage to ¬
do shell script awkCommand & "<<<" & CountriesTable's quoted form
display notification notificationMessage subtitle "US COVID-19 UPDATE" sound name "glass"
NOTE: The code used to determine when the page in Safari has finished loading works in macOS Mojave and later, however, for macOS High Sierra and some earlier versions, add the words buttons of in front of UI elements ... in the repeat until exists ¬ ... code.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

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.

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

how to use automator to send new tweet?

relating to this post, https://apple.stackexchange.com/questions/70585/applescript-opens-new-window-for-everything-when-run.
I wonder if i can highlight the selected text and run this service, can i have the selected text in the new tweet textbox?
Here's the current codes:
activate application "Tweetbot"
tell application "System Events"
tell process "Tweetbot"
repeat until exists
delay 0.4
end repeat
set frontmost to true
delay 0.2
keystroke "n" using command down
end tell
end tell
http://i.stack.imgur.com/aahdK.png
http://i.stack.imgur.com/pHtkX.png
You can pass the selected text as a variable in Automator and use UI scripting to set the contents of the text field.
on run {input, parameters}
activate application "Tweetbot"
tell application "System Events" to tell process "Tweetbot"
keystroke "n" using command down
set value of text area 1 of scroll area 1 of window 1 to (input as text)
end tell
end run
If you run the script with a shortcut that has other modifier keys than command, try replacing keystroke "n" using command down with click menu item "New Tweet" of menu 1 of menu bar item "Tweet" of menu bar 1.

Problem with pasting

I'm trying to write an apple scrip to search Sparrow (mail client for Mac)
Here is the script:
on run argv
tell application "Sparrow"
activate
end tell
tell application "System Events"
key code 3 using {option down, command down}
keystroke argv
end tell
end run
The problem is that I want the script to take an argument on run so that I can supply it with what to search for, but I can't get it to pastet it out.
argv is always initialized to a list.
You cannot keystroke a list (you have to coerce each item to a string first).
You can never tell the exact number of parameters that will be sent to the script, so a better route would be to iterate through the list and do whatever needs to be done, as shown below:
tell application "System Events"
tell process "Sparrow"
key code 3 using {command down, option down}
repeat with this_item in argv
keystroke (this_item as string)
end repeat
end tell
end tell
#Runar
The script is implying that Sparrow is already activated.
You can't do this as written (the result of every text item of argv is still a list). However, if you coerce the result into a string, this will work, but it will squash everything together (assuming AppleScript's text item delimiters is ""). If you set AppleScript's text item delimiters to space, then this would actually be better than the previous script...
on run argv
tell application "Sparrow" to activate
tell application "System Events"
tell process "Sparrow" --implying Sparrow is already activated
set prevTIDs to AppleScript's text item delimiters
key code 3 using {command down, option down}
set AppleScript's text item delimiters to space
keystroke (every text item of argv) as string
set AppleScript's text item delimiters to prevTIDs
end tell
end tell
end run

Resources