0

I'm editing this again to show the latest revision. Thanks to everyone's help here and some digging of my own, I've been able to get this to run in the background by avoiding the use of "system events"

Big thanks to everyone here, especially @Tetsujin for all the help!

My final step on this is to have it update 2 different tables in the same sheet. 1 at 10 minute intervals and 1 with 1 minute intervals. Will try my luck at it and post back if I get stuck.

set myTime to time of (current date) as string
set myAvg to 1.008

on roundThis(n, numDecimals)
    set x to 10 ^ numDecimals
    (((n * x) + 0.5) div 1) / x
end roundThis

repeat until myTime = 75600 -- stop at 9pm local time
    set myTime to (round ((time of date ((current date) as string)) / 60) rounding down) mod 10 = 0 --every 10 minutes
    if myTime then
        say "getting data"
        quit application "Numbers"
        delay 1
        
        tell application "Numbers" to open POSIX file "/Users/steve 1/Documents/document.numbers"
        delay 1
        
        --tell application "Numbers" to activate --only need this for testing
        
        tell application "Numbers"
            tell the table 1 of sheet 1 of document "document"
                set myAvg to the value of cell "C36"
            end tell
        end tell
        say roundThis(myAvg * 100, 2)
        
        tell application "Numbers"
            tell the table 1 of sheet 1 of document "document"
                add column before the range "E1:E2"
                set the value of cell "E1" to time string of (current date)
                repeat with i from 2 to the count of cells of column "E"
                    set the value of cell i of column "E" to (the value of cell i of column "C")
                end repeat
            end tell
        end tell
    end if
    delay 15 --less than 15 will cause it to get double triggered.
end repeat```
6
  • 1
    Can you put display dialog time string of (current date) as text right after 'set my time to…'. I'd like to see what string is actually being tested. Also, you have a couple of typos in your script. Your 'mycolumn' for AB actually shows 'Abraços, St'. And you have two '11:02:00 AM'. Finally, your 'numbers' section is really the best way to move data around. Getting the value of the source cells and then setting the target cells' values would be more reliable.
    – Mockman
    Aug 13, 2021 at 1:51
  • 1
    Long winded? Why not just do a check that the minutes is equal to 30? Or >30 <32?
    – Solar Mike
    Aug 13, 2021 at 5:56
  • Yes that would definitely cut out some lines but my goal is to make this adjustable so I can run it at any interval. Maybe there is a MOD function in AppleScript that I can use to find intervals like every 5 minutes or 10 minutes? Would that work? Aug 13, 2021 at 9:47
  • Thanks Mockman. That was a quickkey that changed AB. Need to turn that off when I'm coding. You are saying my numbers system is the best way or did you mean NOT the best way? Aug 13, 2021 at 9:51
  • 2
    You are getting the error because you are comparing an integer against a string. -- set myTime to (time of date ((current date) as string)) returns an integer , the value of which is the seconds since midnight, e.g. 33348, and AppleScript cannot convert the string, e.g., "8:59:00 PM" to an integer in order to compare the two. Aug 13, 2021 at 13:17

1 Answer 1

1

This is a kind of half answer, just to be able to show the code properly.
You can eliminate all that redundancy with time comparisons using

set myTime to (time of date ((current date) as string))
if myTime & (round (myTime / 60) rounding down) mod 30 = 0 then
    --do function
end if

This will run the function if the time is :00 or :30 minutes. The rounding is because otherwise it would be triggered between :59.30 & :00.30 [& same on the half hour]


After comments
For some reason I cannot fathom, as written that does not return a boolean comparison. This version does [times shortened for patience reasons]…

repeat 60 times
    set myTime to (round ((time of date ((current date) as string)) / 60) rounding down) mod 5 = 0
    if myTime then
        display dialog "Triggered" buttons {"OK"}
    else
        display notification myTime
    end if
    delay 15
end repeat
15
  • I want to use myTime as the column header but not sure how to convert myTime to something like 8:30 PM Aug 13, 2021 at 11:09
  • I thought this would work but also getting an error. set myTimeNum to (time of date (current date)) Result: error "Can’t get date (date \"Friday, August 13, 2021 at 8:14:19 AM\")." number -1728 from date (date "Friday, August 13, 2021 at 8:14:19 AM") Aug 13, 2021 at 11:14
  • 1
    as string, like in the example & yes, mod [any number of minutes] = 0 The number returned by that function is seconds since midnight.
    – Tetsujin
    Aug 13, 2021 at 12:47
  • 1
    No. You can only paste in the foreground.
    – Tetsujin
    Aug 13, 2021 at 13:47
  • 1
    Thank you and sorry about that. I'm learning as I go. :-) Aug 14, 2021 at 17:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .