AppleScript не запускается из календаря - PullRequest
0 голосов
/ 14 ноября 2018

Я сделал следующий код, чтобы открыть веб-сайт, вставить необходимый текст, нажать «Просмотр» и загрузить полученный документ.Он работает отлично, если я нажимаю на него вручную.Это также работает, если я планирую событие календаря, чтобы вызвать его, когда я сижу перед моим Mac.Я пробовал каждую комбинацию держать дисплей вкл / выкл и экран разблокирован / заблокирован.По какой-то причине он не работает в 5:30 и 14:00, когда я запускаю его.Я проверил, чтобы диск и Mac тоже не спали.Будем очень благодарны за любые мысли о том, что я делаю неправильно.

--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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...