AppleScript does not run from calendar










0















I made the following code to open a website, insert some required text, click "View", and download the resulting document. It runs perfectly if I manually click on it. It also works if I schedule a calendar event to call it when I am sitting in front of my Mac. I have tried every combination of keeping the display on/off and screen unlocked/locked. For some reason, it doesn't seem to work at 5:30am and 2:00pm when I set it to run. I have checked to make sure the disk and Mac are not going to sleep as well. Any thoughts on what I am doing wrong would be greatly appreciated.



--Quit Safari before the program starts
tell application "Safari"
close every window
end tell

--empty "Downloads" folder
tell application "System Events"
delete (every file of folder ("/Users/97pubs/Downloads"))
end tell

--opens DINS website
tell application "Safari"
make new document with properties URL:"https://www.notams.faa.gov/dinsQueryWeb/"
activate
ignoring white space
tell front document to repeat until its text contains "About DINS"
end repeat
end ignoring
log "Finished loading"
end tell

delay 2

--Function to press DoD banner on DINS website
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName

clickClassName("ui-state-default", 0)

delay 2

--Function to input ICAO abbreviations on the page
to inputByName(theName, num, theValue)
tell application "Safari"
do JavaScript "
document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" in document 1
end tell
end inputByName

inputByName("retrieveLocId", 0, "kmaf")

delay 2

--Function to click "View NOTAMs" on page
to clickName(theName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByName('" & theName & "')[" & elementnum & "].click();" in document 1
end tell
end clickName

--Clicks "View NOTAMs" on page
clickName("submit", 0)

--Waits for next window to completely load before continuing
tell current application
tell application "Safari"
tell window 1
repeat until (exists tab 2)
delay 1
end repeat
ignoring white space
tell front tab to repeat until its text contains "DINS Disclaimer"
end repeat
end ignoring
end tell
end tell
end tell

delay 1

--Clicks "Save all NOTAMs" on page
clickName("button", 1)

delay 2

--Waits for download to complete before continuing
set someFolder to "/Users/path/to/save/location/" -- put your download folder path here
if not (waitForFilesToCopy into someFolder for (10 * minutes)) then
display alert "Error with downloads" message "The downloads did not complete in the time allowed."
if button returned of the result is "Cancel" then error -128
end if
to waitForFilesToCopy into theFolder for timeToWait
(*
waits up to the timeToWait for files to be copied/downloaded to theFolder the test is based on the size of the folder not changing after several seconds the rough timeToWait may need to be adjusted if copying several files/folders parameters - theFolder [mixed]: the folder to check timeToWait [integer]: a maximum timeout value in seconds returns [boolean]: true if copy/download finished, false if timeout
*)
set theFolder, possible, interval to theFolder as text, false, 2 -- change the check interval as desired
tell application "System Events" to set currentSize to size of disk item theFolder -- get initial size
repeat (timeToWait div interval) times -- check every interval seconds
delay interval
tell application "System Events" to set newSize to size of disk item theFolder -- recheck size
if (newSize is equal to currentSize) then
if possible then -- no change since last time
return true -- success
else -- one more time...
set possible to true
end if
else -- update size & possible switch
set currentSize, possible to newSize, false
end if
end repeat
return false -- fail (timeout)
end waitForFilesToCopy

--Rename PDF to "5 Local NOTAMs.pdf"
tell application "System Events" to set name of file "/Users/path/to/save/location/temp.pdf" to "5 Local NOTAMs.pdf"

--Move NOTAMs to "CrewPapers" folder
tell application "Finder"
move POSIX file "/Users/path/to/save/location/5 Local NOTAMs.pdf" to POSIX file "/Users/path/to/new/location" with replacing
end tell

--Close Safari and Preview windows
tell application "Safari"
close every window
end tell
tell application "Preview"
close every window
end tell









share|improve this question






















  • Try using cron or launchd perhaps.

    – matt
    Nov 14 '18 at 18:24











  • This looks like a script assembled from a couple of separate scripts, which is an excellent way to construct scripts at the beginning, as long as you then go through it and work out what you can merge/replace/simplify to both improve your scripting proficiency and to make your script more readable and easier to debug. I don't have a solution to your specific query, but my first inclination would be to merge together all of your Safari blocks (except the final one that closes the windows, which has to stay where it is), which includes getting rid of those handlers, which aren't needed.

    – CJK
    Nov 14 '18 at 23:43











  • I'd then merge the System Events blocks (except the first one, which needs to stay where it is), plus convert the Finder command to a System Events one and merge that. This would also mean breaking open the waitForFilesToCopy handler to merge with the other blocks, if that handler is really how you want to monitor for the downloaded file (I'd use a Folder Action for this step).

    – CJK
    Nov 14 '18 at 23:50















0















I made the following code to open a website, insert some required text, click "View", and download the resulting document. It runs perfectly if I manually click on it. It also works if I schedule a calendar event to call it when I am sitting in front of my Mac. I have tried every combination of keeping the display on/off and screen unlocked/locked. For some reason, it doesn't seem to work at 5:30am and 2:00pm when I set it to run. I have checked to make sure the disk and Mac are not going to sleep as well. Any thoughts on what I am doing wrong would be greatly appreciated.



--Quit Safari before the program starts
tell application "Safari"
close every window
end tell

--empty "Downloads" folder
tell application "System Events"
delete (every file of folder ("/Users/97pubs/Downloads"))
end tell

--opens DINS website
tell application "Safari"
make new document with properties URL:"https://www.notams.faa.gov/dinsQueryWeb/"
activate
ignoring white space
tell front document to repeat until its text contains "About DINS"
end repeat
end ignoring
log "Finished loading"
end tell

delay 2

--Function to press DoD banner on DINS website
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName

clickClassName("ui-state-default", 0)

delay 2

--Function to input ICAO abbreviations on the page
to inputByName(theName, num, theValue)
tell application "Safari"
do JavaScript "
document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" in document 1
end tell
end inputByName

inputByName("retrieveLocId", 0, "kmaf")

delay 2

--Function to click "View NOTAMs" on page
to clickName(theName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByName('" & theName & "')[" & elementnum & "].click();" in document 1
end tell
end clickName

--Clicks "View NOTAMs" on page
clickName("submit", 0)

--Waits for next window to completely load before continuing
tell current application
tell application "Safari"
tell window 1
repeat until (exists tab 2)
delay 1
end repeat
ignoring white space
tell front tab to repeat until its text contains "DINS Disclaimer"
end repeat
end ignoring
end tell
end tell
end tell

delay 1

--Clicks "Save all NOTAMs" on page
clickName("button", 1)

delay 2

--Waits for download to complete before continuing
set someFolder to "/Users/path/to/save/location/" -- put your download folder path here
if not (waitForFilesToCopy into someFolder for (10 * minutes)) then
display alert "Error with downloads" message "The downloads did not complete in the time allowed."
if button returned of the result is "Cancel" then error -128
end if
to waitForFilesToCopy into theFolder for timeToWait
(*
waits up to the timeToWait for files to be copied/downloaded to theFolder the test is based on the size of the folder not changing after several seconds the rough timeToWait may need to be adjusted if copying several files/folders parameters - theFolder [mixed]: the folder to check timeToWait [integer]: a maximum timeout value in seconds returns [boolean]: true if copy/download finished, false if timeout
*)
set theFolder, possible, interval to theFolder as text, false, 2 -- change the check interval as desired
tell application "System Events" to set currentSize to size of disk item theFolder -- get initial size
repeat (timeToWait div interval) times -- check every interval seconds
delay interval
tell application "System Events" to set newSize to size of disk item theFolder -- recheck size
if (newSize is equal to currentSize) then
if possible then -- no change since last time
return true -- success
else -- one more time...
set possible to true
end if
else -- update size & possible switch
set currentSize, possible to newSize, false
end if
end repeat
return false -- fail (timeout)
end waitForFilesToCopy

--Rename PDF to "5 Local NOTAMs.pdf"
tell application "System Events" to set name of file "/Users/path/to/save/location/temp.pdf" to "5 Local NOTAMs.pdf"

--Move NOTAMs to "CrewPapers" folder
tell application "Finder"
move POSIX file "/Users/path/to/save/location/5 Local NOTAMs.pdf" to POSIX file "/Users/path/to/new/location" with replacing
end tell

--Close Safari and Preview windows
tell application "Safari"
close every window
end tell
tell application "Preview"
close every window
end tell









share|improve this question






















  • Try using cron or launchd perhaps.

    – matt
    Nov 14 '18 at 18:24











  • This looks like a script assembled from a couple of separate scripts, which is an excellent way to construct scripts at the beginning, as long as you then go through it and work out what you can merge/replace/simplify to both improve your scripting proficiency and to make your script more readable and easier to debug. I don't have a solution to your specific query, but my first inclination would be to merge together all of your Safari blocks (except the final one that closes the windows, which has to stay where it is), which includes getting rid of those handlers, which aren't needed.

    – CJK
    Nov 14 '18 at 23:43











  • I'd then merge the System Events blocks (except the first one, which needs to stay where it is), plus convert the Finder command to a System Events one and merge that. This would also mean breaking open the waitForFilesToCopy handler to merge with the other blocks, if that handler is really how you want to monitor for the downloaded file (I'd use a Folder Action for this step).

    – CJK
    Nov 14 '18 at 23:50













0












0








0








I made the following code to open a website, insert some required text, click "View", and download the resulting document. It runs perfectly if I manually click on it. It also works if I schedule a calendar event to call it when I am sitting in front of my Mac. I have tried every combination of keeping the display on/off and screen unlocked/locked. For some reason, it doesn't seem to work at 5:30am and 2:00pm when I set it to run. I have checked to make sure the disk and Mac are not going to sleep as well. Any thoughts on what I am doing wrong would be greatly appreciated.



--Quit Safari before the program starts
tell application "Safari"
close every window
end tell

--empty "Downloads" folder
tell application "System Events"
delete (every file of folder ("/Users/97pubs/Downloads"))
end tell

--opens DINS website
tell application "Safari"
make new document with properties URL:"https://www.notams.faa.gov/dinsQueryWeb/"
activate
ignoring white space
tell front document to repeat until its text contains "About DINS"
end repeat
end ignoring
log "Finished loading"
end tell

delay 2

--Function to press DoD banner on DINS website
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName

clickClassName("ui-state-default", 0)

delay 2

--Function to input ICAO abbreviations on the page
to inputByName(theName, num, theValue)
tell application "Safari"
do JavaScript "
document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" in document 1
end tell
end inputByName

inputByName("retrieveLocId", 0, "kmaf")

delay 2

--Function to click "View NOTAMs" on page
to clickName(theName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByName('" & theName & "')[" & elementnum & "].click();" in document 1
end tell
end clickName

--Clicks "View NOTAMs" on page
clickName("submit", 0)

--Waits for next window to completely load before continuing
tell current application
tell application "Safari"
tell window 1
repeat until (exists tab 2)
delay 1
end repeat
ignoring white space
tell front tab to repeat until its text contains "DINS Disclaimer"
end repeat
end ignoring
end tell
end tell
end tell

delay 1

--Clicks "Save all NOTAMs" on page
clickName("button", 1)

delay 2

--Waits for download to complete before continuing
set someFolder to "/Users/path/to/save/location/" -- put your download folder path here
if not (waitForFilesToCopy into someFolder for (10 * minutes)) then
display alert "Error with downloads" message "The downloads did not complete in the time allowed."
if button returned of the result is "Cancel" then error -128
end if
to waitForFilesToCopy into theFolder for timeToWait
(*
waits up to the timeToWait for files to be copied/downloaded to theFolder the test is based on the size of the folder not changing after several seconds the rough timeToWait may need to be adjusted if copying several files/folders parameters - theFolder [mixed]: the folder to check timeToWait [integer]: a maximum timeout value in seconds returns [boolean]: true if copy/download finished, false if timeout
*)
set theFolder, possible, interval to theFolder as text, false, 2 -- change the check interval as desired
tell application "System Events" to set currentSize to size of disk item theFolder -- get initial size
repeat (timeToWait div interval) times -- check every interval seconds
delay interval
tell application "System Events" to set newSize to size of disk item theFolder -- recheck size
if (newSize is equal to currentSize) then
if possible then -- no change since last time
return true -- success
else -- one more time...
set possible to true
end if
else -- update size & possible switch
set currentSize, possible to newSize, false
end if
end repeat
return false -- fail (timeout)
end waitForFilesToCopy

--Rename PDF to "5 Local NOTAMs.pdf"
tell application "System Events" to set name of file "/Users/path/to/save/location/temp.pdf" to "5 Local NOTAMs.pdf"

--Move NOTAMs to "CrewPapers" folder
tell application "Finder"
move POSIX file "/Users/path/to/save/location/5 Local NOTAMs.pdf" to POSIX file "/Users/path/to/new/location" with replacing
end tell

--Close Safari and Preview windows
tell application "Safari"
close every window
end tell
tell application "Preview"
close every window
end tell









share|improve this question














I made the following code to open a website, insert some required text, click "View", and download the resulting document. It runs perfectly if I manually click on it. It also works if I schedule a calendar event to call it when I am sitting in front of my Mac. I have tried every combination of keeping the display on/off and screen unlocked/locked. For some reason, it doesn't seem to work at 5:30am and 2:00pm when I set it to run. I have checked to make sure the disk and Mac are not going to sleep as well. Any thoughts on what I am doing wrong would be greatly appreciated.



--Quit Safari before the program starts
tell application "Safari"
close every window
end tell

--empty "Downloads" folder
tell application "System Events"
delete (every file of folder ("/Users/97pubs/Downloads"))
end tell

--opens DINS website
tell application "Safari"
make new document with properties URL:"https://www.notams.faa.gov/dinsQueryWeb/"
activate
ignoring white space
tell front document to repeat until its text contains "About DINS"
end repeat
end ignoring
log "Finished loading"
end tell

delay 2

--Function to press DoD banner on DINS website
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName

clickClassName("ui-state-default", 0)

delay 2

--Function to input ICAO abbreviations on the page
to inputByName(theName, num, theValue)
tell application "Safari"
do JavaScript "
document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" in document 1
end tell
end inputByName

inputByName("retrieveLocId", 0, "kmaf")

delay 2

--Function to click "View NOTAMs" on page
to clickName(theName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByName('" & theName & "')[" & elementnum & "].click();" in document 1
end tell
end clickName

--Clicks "View NOTAMs" on page
clickName("submit", 0)

--Waits for next window to completely load before continuing
tell current application
tell application "Safari"
tell window 1
repeat until (exists tab 2)
delay 1
end repeat
ignoring white space
tell front tab to repeat until its text contains "DINS Disclaimer"
end repeat
end ignoring
end tell
end tell
end tell

delay 1

--Clicks "Save all NOTAMs" on page
clickName("button", 1)

delay 2

--Waits for download to complete before continuing
set someFolder to "/Users/path/to/save/location/" -- put your download folder path here
if not (waitForFilesToCopy into someFolder for (10 * minutes)) then
display alert "Error with downloads" message "The downloads did not complete in the time allowed."
if button returned of the result is "Cancel" then error -128
end if
to waitForFilesToCopy into theFolder for timeToWait
(*
waits up to the timeToWait for files to be copied/downloaded to theFolder the test is based on the size of the folder not changing after several seconds the rough timeToWait may need to be adjusted if copying several files/folders parameters - theFolder [mixed]: the folder to check timeToWait [integer]: a maximum timeout value in seconds returns [boolean]: true if copy/download finished, false if timeout
*)
set theFolder, possible, interval to theFolder as text, false, 2 -- change the check interval as desired
tell application "System Events" to set currentSize to size of disk item theFolder -- get initial size
repeat (timeToWait div interval) times -- check every interval seconds
delay interval
tell application "System Events" to set newSize to size of disk item theFolder -- recheck size
if (newSize is equal to currentSize) then
if possible then -- no change since last time
return true -- success
else -- one more time...
set possible to true
end if
else -- update size & possible switch
set currentSize, possible to newSize, false
end if
end repeat
return false -- fail (timeout)
end waitForFilesToCopy

--Rename PDF to "5 Local NOTAMs.pdf"
tell application "System Events" to set name of file "/Users/path/to/save/location/temp.pdf" to "5 Local NOTAMs.pdf"

--Move NOTAMs to "CrewPapers" folder
tell application "Finder"
move POSIX file "/Users/path/to/save/location/5 Local NOTAMs.pdf" to POSIX file "/Users/path/to/new/location" with replacing
end tell

--Close Safari and Preview windows
tell application "Safari"
close every window
end tell
tell application "Preview"
close every window
end tell






applescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 18:19









MrKGadoMrKGado

146




146












  • Try using cron or launchd perhaps.

    – matt
    Nov 14 '18 at 18:24











  • This looks like a script assembled from a couple of separate scripts, which is an excellent way to construct scripts at the beginning, as long as you then go through it and work out what you can merge/replace/simplify to both improve your scripting proficiency and to make your script more readable and easier to debug. I don't have a solution to your specific query, but my first inclination would be to merge together all of your Safari blocks (except the final one that closes the windows, which has to stay where it is), which includes getting rid of those handlers, which aren't needed.

    – CJK
    Nov 14 '18 at 23:43











  • I'd then merge the System Events blocks (except the first one, which needs to stay where it is), plus convert the Finder command to a System Events one and merge that. This would also mean breaking open the waitForFilesToCopy handler to merge with the other blocks, if that handler is really how you want to monitor for the downloaded file (I'd use a Folder Action for this step).

    – CJK
    Nov 14 '18 at 23:50

















  • Try using cron or launchd perhaps.

    – matt
    Nov 14 '18 at 18:24











  • This looks like a script assembled from a couple of separate scripts, which is an excellent way to construct scripts at the beginning, as long as you then go through it and work out what you can merge/replace/simplify to both improve your scripting proficiency and to make your script more readable and easier to debug. I don't have a solution to your specific query, but my first inclination would be to merge together all of your Safari blocks (except the final one that closes the windows, which has to stay where it is), which includes getting rid of those handlers, which aren't needed.

    – CJK
    Nov 14 '18 at 23:43











  • I'd then merge the System Events blocks (except the first one, which needs to stay where it is), plus convert the Finder command to a System Events one and merge that. This would also mean breaking open the waitForFilesToCopy handler to merge with the other blocks, if that handler is really how you want to monitor for the downloaded file (I'd use a Folder Action for this step).

    – CJK
    Nov 14 '18 at 23:50
















Try using cron or launchd perhaps.

– matt
Nov 14 '18 at 18:24





Try using cron or launchd perhaps.

– matt
Nov 14 '18 at 18:24













This looks like a script assembled from a couple of separate scripts, which is an excellent way to construct scripts at the beginning, as long as you then go through it and work out what you can merge/replace/simplify to both improve your scripting proficiency and to make your script more readable and easier to debug. I don't have a solution to your specific query, but my first inclination would be to merge together all of your Safari blocks (except the final one that closes the windows, which has to stay where it is), which includes getting rid of those handlers, which aren't needed.

– CJK
Nov 14 '18 at 23:43





This looks like a script assembled from a couple of separate scripts, which is an excellent way to construct scripts at the beginning, as long as you then go through it and work out what you can merge/replace/simplify to both improve your scripting proficiency and to make your script more readable and easier to debug. I don't have a solution to your specific query, but my first inclination would be to merge together all of your Safari blocks (except the final one that closes the windows, which has to stay where it is), which includes getting rid of those handlers, which aren't needed.

– CJK
Nov 14 '18 at 23:43













I'd then merge the System Events blocks (except the first one, which needs to stay where it is), plus convert the Finder command to a System Events one and merge that. This would also mean breaking open the waitForFilesToCopy handler to merge with the other blocks, if that handler is really how you want to monitor for the downloaded file (I'd use a Folder Action for this step).

– CJK
Nov 14 '18 at 23:50





I'd then merge the System Events blocks (except the first one, which needs to stay where it is), plus convert the Finder command to a System Events one and merge that. This would also mean breaking open the waitForFilesToCopy handler to merge with the other blocks, if that handler is really how you want to monitor for the downloaded file (I'd use a Folder Action for this step).

– CJK
Nov 14 '18 at 23:50












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306505%2fapplescript-does-not-run-from-calendar%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306505%2fapplescript-does-not-run-from-calendar%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto