Friday, October 19, 2018

#1 2018-03-13 01:14:39 pm

machover99
Member
Registered: 2011-03-17
Posts: 49

Pause & First Run: Would someone give me a hand?

I’ve been working on a script (for years) that launches a pdf and inputs its open password. The purpose of the script is to hide the process of inputting the PW. I finished the script a few months ago - except I've got one problem left that I can't seem to lick. If anyone can give me a hand with this last problem, I’d be grateful.

The first time the script runs (and only the first time) – after a restart, log out or  initial boot - the pauses are a mess. Seems like they’re either lengthened (at the beginning of the script) or shortened (at the end). This mostly happens on Yosemite but on the other OS’s as well. When the pauses don't work, the script fails to achieve its purpose (see below).

Aside from this one problem, the script works consistently  on 10.9, 10.10, 10.11 & 10.12.

One fix that helped (but not completely): I divided the script into 3 parts, a primary and two helper scripts.   The following are the two helper scripts. They finish it.

The first helper script (comes after the main script):

Applescript:


--display dialog one moment giving up after 5--
tell application "Finder"
   activate
end tell

delay 0.5

tell application "Finder" to display dialog "One moment..." giving up after 5 buttons {"Cancel"} default button ¬
   "Cancel"

delay 0.1

--open helper_2--
tell application "Finder"
   open file "helper_2" of folder "MySoftware" of folder "Applications" of startup disk ¬
end tell

The second helper script finishes it:

Applescript:


set keyvalue to “abc” & return
set AppName to POSIX file "/Applications/Adobe Reader.app"

--open file--
tell application "Finder"
   open file “MyPDF” of folder "MySoftware" of folder "Applications" of startup disk ¬
       using AppName
   
end tell

delay 0.1

--launch dashboard--
ignoring application responses
   tell application "System Events"
       tell application "Finder"
           tell application "Dashboard"
               activate
           end tell
       end tell
   end tell
end ignoring

delay 0.01

--keystrokePW--
tell application "System Events" to keystroke keyvalue

I added the first helper so the script can catch up with itself (during the first run). (I think it does this but I’m not really sure). The 2nd helper  inputs the PW while shifting to dashboard. The purpose of this part of the script: It uses the shift to dashboard to hide  the process of inputting the PW from the user - which is the main purpose of the script.

When the script is run after a restart, the last part of the script moves into dashboard too rapidly and then hangs there. Whereas every time thereafter, the script moves into dashboard and then out, back to the desktop where the pdf appears, with the PW already inputted (and the PW input is concealed.)

The reason my first run problem is a headache: It's the only time the script fails to hide the process of inputting the PW.

I recently noticed, if I wait a long time after the computer boots up (15, 20 minutes), my first run problem doesn’t occur. So, my first question:

Is there a way for a script to monitor how long it has been since the computer booted  or to determine, has the computer completely booted.  If this is possible, an if/then statement be used: If the computer hasn’t completely booted, then present the dialog box, “Please wait until your computer is booted before launching…” else proceed with script.

This would fix my problem.

Last question: The visual effect of shifting into dashboard is the same ‘sliding door’ effect you see when iTunes is enabled. But on the first run, the 'sliding door' moves too rapidly. Whereas on iTunes, the ’sliding door’ moves slowly – more slowly than it ever does for my script when moving into dashboard. Q: Is there a way to control this movement using AS?

I’m still an applescript dabbler but  know more than I did some years back when I first started - and most of my learning has come from reading the posts on the forum…thank you for that…’preciate it…

Offline

 

#2 2018-06-26 09:13:04 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 2

Re: Pause & First Run: Would someone give me a hand?

Tightened up your code a bit and removed some unnecessary Items and such.  Your version of Acrobat reader Is different than mine, so I had to adjust the code a little bit to work with my version. 

This code works for me using the latest version of macOS High Sierra

Applescript:

display dialog "One moment..." giving up after 5 buttons {"Cancel"} default button ¬
   "Cancel"

set keyvalue to "test"
set AppName to "/Applications/Adobe Acrobat Reader DC.app"
set fileName to "Macintosh HD:Applications:MySoftware:MyPDF.pdf"

tell application AppName to launch

ignoring application responses
   tell application AppName
       open file fileName
   end tell
end ignoring

delay 0.1

ignoring application responses
   tell application "Dashboard" to activate
end ignoring

delay 0.1

tell application "System Events"
   set value of text field 1 of window "Password" of application process ¬
       "Acrobat Reader" to keyvalue
   delay 0.1
   perform action "AXPress" of button "OK" of window ¬
       "Password" of application process "Acrobat Reader"
end tell

tell application "System Events" to key code 53


Filed under: applescript

Offline

 

#3 2018-06-27 06:43:24 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2807
Website

Re: Pause & First Run: Would someone give me a hand?

machover99 wrote:

The first time the script runs (and only the first time) – after a restart, log out or  initial boot - the pauses are a mess. Seems like they’re either lengthened (at the beginning of the script) or shortened (at the end). This mostly happens on Yosemite but on the other OS’s as well. When the pauses don't work, the script fails to achieve its purpose (see below).



Timers in Yosemite is an known problem and corrected in El Capitan including the delay command. A workaround is using other times like shell's sleep command.

Applescript:

do shell script "sleep 5"

To show an dialog for 5 seconds you can use osascript and run the dialog in the background and kill that process after 5 seconds like this:

Applescript:

do shell script "osascript -e 'display dialog \"A moment please…\"' &
scriptPID=$!
sleep 5
kill $scriptPID &>/dev/null
exit 0"

Last edited by DJ Bazzie Wazzie (2018-06-27 06:44:42 am)

Offline

 

#4 2018-06-28 05:58:10 pm

machover99
Member
Registered: 2011-03-17
Posts: 49

Re: Pause & First Run: Would someone give me a hand?

Thank you for this.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)