Apple launches Apple Store app in India

The Apple Store app provides customers with the most personalized way to shop for Apple’s innovative lineup of products and services. Learn more >

You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Apple script - how I can insert number in active Numbers cell?

Hi, Community.

I need to insert variable's value (theRate) in active Numbers cell currently selected in opened document.

It is currency rate I get from National Bank site. I want to select needed cell and choose Apple Script from Scripts Menu – currency rate should be inserted in this cell.

Tried in several ways but get uninformative -10006 error which says "The write operation was denied."


The second problem – I can't get needed value (<rate> tag) via XML set of commands without errors, so I just croped XML result I get from National Bank site with "word" and "paragraph".


Here is the part of working script:


set {year:y, month:m, day:d} to (current date)

set day_str to text -1 thru -2 of ("00" & d)

set mon_str to text -1 thru -2 of ("00" & (m * 1))

set theDate to (y & mon_str & day_str) as string


set theURL to "https://bank.gov.ua/NBU_Exchange/exchange_site?date=" & theDate & "&valcode=usd"

set thePrice to do shell script "curl -s " & quoted form of theURL

set theRate to word 4 of paragraph 9 of thePrice


on findAndReplaceInText(theText, theSearchString, theReplacementString)

set AppleScript's text item delimiters to theSearchString

set theTextItems to every text item of theText

set AppleScript's text item delimiters to theReplacementString

set theText to theTextItems as string

set AppleScript's text item delimiters to ""

return theText

end findAndReplaceInText


set theText to findAndReplaceInText(theRate, ".", ",")


Thanks for help.

MacBook Pro 15″, macOS 12.7

Posted on Jan 17, 2025 1:14 AM

Reply
4 replies

Jan 17, 2025 7:13 PM in response to iStuntman

You can put a value into the selected cell (the first cell of the selection range) by doing something like this:


set theXML to "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<exchange>
  <currency>
    <exchangedate>17.01.2025</exchangedate>
    <r030>840</r030>
    <cc>USD</cc>
    <txt>Долар США</txt>
    <enname>US Dollar</enname>
    <rate>42.1729</rate>
    <units>1</units>
    <rate_per_unit>42.1729</rate_per_unit>
    <group>1</group>
    <calcdate>16.01.2025</calcdate>
</currency>
</exchange>"
set theRate to word 4 of paragraph 5 of theXML

tell front document of application "Numbers"
	tell active sheet
		tell (first table whose class of selection range is range)
			set value of first cell of selection range to theRate
		end tell
	end tell
end tell



SG

Jan 17, 2025 8:35 AM in response to SGIII

Thanks for reply, SGIII!


If you just run these two:

set theURL to "https://bank.gov.ua/NBU_Exchange/exchange_site?date=20250117&valcode=usd"

set thePrice to do shell script "curl -s " & quoted form of theURL


... the answer will be:

"<?xml version=\"1.0\" encoding=\"utf-8\"?>

<exchange>

  <currency>

    <exchangedate>17.01.2025</exchangedate>

    <r030>840</r030>

    <cc>USD</cc>

    <txt>Долар США</txt>

    <enname>US Dollar</enname>

    <rate>42.1729</rate>

    <units>1</units>

    <rate_per_unit>42.1729</rate_per_unit>

    <group>1</group>

    <calcdate>16.01.2025</calcdate>

</currency>

</exchange>"


Thanks.

Apple script - how I can insert number in active Numbers cell?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.