How to Populate Pages file from Excel Worksheet using Applescript - macos

I'm trying to populate a template from data in an Excel spreadsheet. This is my first foray into Applescript. My plan is to have unique text strings in the template (i.e. Value_1, Value_2, etc) then store each value in a variable and find and replace each variable with each string.
I would very much appreciate any help; here is where I'm at:
tell application "Microsoft Excel"
tell active workbook
activate object worksheet "Page 2"
copy range range "B7" of active sheet
// Store as variable?
end tell
end tell
tell application "Pages"
activate
tell application "System Events"
keystroke "v" using command down
// Find and replace?
end tell
end tell

Something like this will do what you want :
tell application "Microsoft Excel"
tell active workbook
activate object worksheet "Sheet2"
set B7_Value to string value of range "B7" of active sheet
end tell
end tell
tell application "Pages"
set my_text to first document's body text
set temp to do shell script "echo " & quoted form of my_text & " | sed -e 's/B7_Value/" & B7_Value & "/g'"
set first document's body text to temp
end tell
This script does not keep formatting. If you want to keep formatting perhaps something like this :
tell application "Microsoft Excel"
tell active workbook
activate object worksheet "Sheet2"
set B7_Value to string value of range "B7" of active sheet
end tell
end tell
tell application "Pages"
activate
tell application "System Events"
keystroke "f" using command down
set the clipboard to "B7_Value"
keystroke "v" using command down
delay 0.1
keystroke tab
delay 0.1
set the clipboard to B7_Value
keystroke "v" using command down
delay 0.1
keystroke tab
delay 0.1
keystroke space
delay 0.1
key code 53 -- escape
end tell
end tell
For this to work you will need to enable System Preferences --> Keyboard --> All controls.

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.

How to set clipboard with value of selected cell in Numbers using AppleScript

I am trying to automate a repeating task between Numbers and Quicken 2017 using AppleScript.
I would like to take the contents of the currently selected cell in Numbers, set the clipboard with that numeric value, and paste that value into the search field in Quicken.
How can one go about doing that with AppleScript?
Example pseudo code to illustrate intent:
tell application "Numbers"
activate
set myCellsValue to value of currently selected cell
set the clipboard to myCellsValue
end tell
tell application "Quicken 2017"
activate
tell application "System Events"
keystroke "f" using {command down}
delay 0.1
keystroke "v" using {command down}
end tell
end tell
You got it 90% correct. I don't use Quicken, but this worked with Microsoft Word:
tell application "Numbers"
activate
tell active sheet to tell application "System Events" to keystroke "c" using command down
end tell
tell application "Quicken 2017"
activate
tell application "System Events"
keystroke "f" using command down
delay 0.1
keystroke "v" using command down
end tell
end tell
I generally like to minimise the amount by which I employ System Events to issue keystrokes or mouse clicks if there’s another way.
Numbers is very much AppleScript-able. I don’t use it personally, but with the help of this site, I’ve pieced together this example script that I wholly admit is untested (I’d appreciate your feedback if you try it out):
tell application "Numbers"
tell the front document to ¬
tell the active sheet to ¬
set CurrentTable to the first table whose class of selection range is range
tell the CurrentTable to get the value of the first cell in the selection range
if the result is not missing value then set the clipboard to the result
end tell
Quicken is also AppleScript-able, but I can’t find a downloadable copy of Quicken’s AppleScript dictionary in order to piece together an equivalent sample of code. However, your Quicken tell block is exactly right for employing System Events to issue a Cmd+V.
However, if you fancy uploading a PDF of the AppleScript dictionary, I can use it to try and draft something more robust.

Applescript Webforms

I am trying to make an applescript that submit a form on mxtoolbox.com that uses AJAX at this link:
http://mxtoolbox.com/EmailHeaders.aspx
tell application "Safari"
activate
tell (make new document) to set URL to "http://mxtoolbox.com/EmailHeaders.aspx"
delay 10
tell application "System Events"
-- Copies text from variable headerText to clipboard and paste. Faster than keystroke headerText
set the clipboard to headerText
keystroke "v" using command down
end tell
end tell
Any ideas?
A suggestion was the following but it still did not work:
tell application "Safari"
activate
tell (make new document) to set URL to "http://mxtoolbox.com/EmailHeaders.aspx"
delay 10
tell application "System Events"
set the clipboard to headerText
keystroke "v" using command down
tell application "Safari"
do JavaScript ("
document.getElementById('ctl00_ContentPlaceHolder1_txtToolInput').value='" & headerText as text) & "';
document.getElementById('ctl00_ContentPlaceHolder1_btnAction').click();
" in document 1
end tell
end tell
end tell
You could do this with JavaScript:
set headerText to "test"
tell application "Safari"
activate
tell (make new document) to set URL to "http://mxtoolbox.com/EmailHeaders.aspx"
delay 5
do JavaScript "
document.getElementById('ctl00_ContentPlaceHolder1_txtToolInput').value=" & quoted form of headerText & ";
document.getElementById('ctl00_ContentPlaceHolder1_btnAction').click()" in document 1
end tell

AppleScript to Save Preview Document

I'm using a third party application to copy an image to the clipboard. I would then like to perform an AppleScript that opens Preview, creates a New Document which will then use the clipboard as the content, then save this to a specified location.
I'm 99% of the way there - just can't get the document to save. I'm on OS X 10.9.5.
Here's my script so far:
set the save_location to the quoted form of "/Users/freddy/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
activate
save front document as "PNG" in POSIX file save_location
end tell
I can't find the correct syntax for saving a Preview document. It will be the only document open at the time.
Try the following - note the comments:
# Do not use `quoted form of` - only needed and useful with `do shell script`
set the save_location to "/Users/jdoe/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
# Wait until the new window appears.
# Trying to save before the window has appeared will fail.
# Note: - This assumes that NO window was initially open.
# - The code should be made more robust to eventually time out.
repeat until (count of windows) > 0
delay 0.3
end repeat
activate
# Save the document; `as "<format>"` doesn't seem to work, but the
# format is *implied* by the filename suffix (extension).
save front document in POSIX file save_location
end tell
This script works to save an image in the clipboard to disk, using Preview in OS 10.11.6 on a 2010 Mac Mini:
--CONDITIONS: Finder's clipboard has an image in it
tell application "Preview"
launch
activate
end tell
delay 2 --tweak the delays to what works
tell application "System Events" --not as elegant as "tell application...to tell process", but clearer, chronologically
keystroke "n" using command down --Preview creates a new window.
delay 1
keystroke "v" using command down --Finder's clipboard image is pasted into Preview window.
end tell
delay 1
tell application "System Events"
set the clipboard to "Joe Blow" --for name of forthcoming Preview file
delay 1
keystroke "w" using command down --Forces prompt of Preview to save image.
delay 1
keystroke "v" using command down --Pastes new filename into Preview's save window.
delay 1
key code 36 --"Return" key saves the new file.
end tell
--RESULT: A new image exists in your default folder, named "Joe Blow.png" (or whatever extension)

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.

Resources