I want to convert rich text cell content of my numbers table to other formats like Markdown or HTML. But how do I get more then just plain text?
Maybe there's a solution how to set the selection range and copy it as rtf to the clipboard.
tell application "Numbers"
get formatted value of cell 2 of row 2 of table 1 of sheet 1 of document of window 1
end tell
Here's a script to select the cell and then copy the contents to the clipboard which preserves the formatting:
tell application "Numbers"
tell table 1 of sheet 1 of document of window 1
activate
set selection range to cell 2 of row "2" --range "B2:B2"
end tell
tell application "System Events"
tell process "Numbers"
keystroke "c" using command down
end tell
end tell
end tell
Related
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®ion=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®ion=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.
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.
I'm trying to duplicate a sheet in a numbers file (I need a new sheet everyday based on a template) using AppleScript.
I'm really new to AppleScript but I'm already using it to fill-in some cells with results I get from a python script I run but this bit is still manual... =/
tell application "Numbers"
tell document 1
set thisSh to make new sheet with properties {name:"May14"}
duplicate sheet "Template" to sheet "May14"
end tell
end tell
The code above returns the error: error "Numbers got an error: Sheets can not be copied." number -1717 and, therefore, my question: Is there a way to duplicate a numbers sheet?
I'm currently running iWork '14 with numbers 3.2
Thanks!
P.S. I also tried duplicate every table of sheet "Template" to sheet "May14" with a similar error: Tables can not be copied.
You have two options. You can build the table in the new sheet programmatically, or you can select and copy the table(s) from the first sheet and copy it(them) in the new sheet. Here is code for the second option. Having not seen a screen grab of your template sheet, you may have to adjust, but this should give you everything you need. If you run into complications, please post a screenshot and description of your template sheet.
Updated to account for a crazy number of tables in the sheet. Updated again to demonstrate how to change the active sheet.
tell application "Numbers"
activate
tell document 1
set tempSheet to first sheet whose name is "My Template"
set active sheet to tempSheet
tell application "System Events"
-- deselect all
keystroke "a" using {command down, shift down}
delay 0.1
-- select all containers (tables, text items, etc.)
keystroke "a" using {command down}
delay 0.1
-- copy the containers
keystroke "c" using {command down}
end tell
delay 0.1
set dateString to (month of (current date)) & (day of (current date)) as string
set thisSheet to make new sheet with properties {name:dateString}
tell thisSheet
delete every table
tell application "System Events"
keystroke "v" using {command down}
end tell
end tell
end tell
end tell
10.7.4 OSX Lion
Applescript
I am working with an application (built in house and has no Applescript dictionary) that has a static text element I want to copy to the clipboard and send to another app but I'm having a hard time getting it to work.
The script I was using for targeting the element looked like this:
Tell application "System Events" to set frontmost of process "*application*" to true
Tell application "System Events"
Tell process "*application*"
Tell static text 1 of tab view 1 scroll area 1 of splitter group 1 of splitter group 1 of splitter group 1 of window 1
keystroke "a" using command down
delay 0.1
keystroke "c" using command down
delay 0.1
end tell
end tell
end tell
end tell
What would happen was that the wrong text from the wrong element was copied to the clipboard every time I clicked in a different spot on the application (there are numerous text fields).
I noticed in UI Accessor/Accessibility Accessor that each UI element in the application has a unique AXIdentifier value when you mouse over them.
Is there anyway to accomplishing what I am trying to do, using AXIdentifier values to target that element and copy the text from it?
Thanks for all the help this is my first post and I hope it was worthy! ~TheLarkInn
You can do this by using AppleScript's filtering. For example, to get the From: pop-up menu in a message composition window in Apple Mail, there is no accessibility description you can match on, however there is a unique AXIdentifier which you can match as follows:
tell application "System Events"
tell application process "Mail"
tell window 1
get first pop up button whose value of attribute "AXIdentifier" is "popup_from"
end tell
end tell
end tell
This is more efficient than looping in AppleScript as it only involves sending one Apple Event to System Events.
I don't think there is a way to directly do what you're trying to do. It seems like you can only access attributes once you have a handle on an element via selector. Here is a very ugly solution that does what you're asking by iterating over all UI elements, but it is really slow with bigger UIs and probably not ideal for any production level code.
tell application "System Events"
tell process "Some Process"
set tElements to entire contents of window "Some Window"
repeat with tElement in tElements
if (exists attribute "AXIdentifier" of tElement) then
if value of attribute "AXIdentifier" of tElement = "Some AXIdentifier" then set tText to value of tElement
end if
end repeat
end tell
end tell
tText
I think using UIElementInspector or Accessibility Inspector from Xcode to build a selector string is the way to go!
Tell application "*application*" to activate
Tell application "System Events"
Tell application process "*application*"
set textStaticTextValue to value of static text 1 of tab view 1 scroll area 1 of splitter group 1 of splitter group 1 of splitter group 1 of window 1
end tell
end tell
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.