Thursday, December 3, 2020

#1 2020-11-21 08:38:46 am

cocoa_on_apple
Member
Registered: 2020-11-20
Posts: 4

Code snippet works in isolation but not when embedded(fill text field)

The Applescript below is meant to do the following : Ask the Skitch app to take a screenshot (with the same parameters as the preceding screenshot, so it is assumed that a dummy screenshot has been made before), and save the screenshot on some location.

Applescript:


tell application "System Events"
   tell process "Skitch"
       set frontmost to true
       keystroke "5" using {command down, shift down, option down} (* Shortcut for `take screenshot` *)
       keystroke "e" using {command down} (* Shortcut for `save` *)
       set value of text field 1 of sheet 1 of window 1 to "my_saved_screenshot"
   end tell
   key code 36
end tell

The code does not works as expected : the filename does not get written in the text field, the file is not saved, and I get the error "System Events got an error: Can’t get text field 1 of sheet 1 of window 1 of process \"Skitch\". Invalid index." number -1719 from text field 1 of sheet 1 of window 1 of process "Skitch".

What is suprising is that if I isolate the offending snippet and put in a new script, it works fine : a file is created with the correct name if I do the follwing after have made the screenshot separately :


Applescript:

tell application "System Events"
   tell process "Skitch"
       set frontmost to true
       keystroke "e" using {command down} (* Shortcut for `save` *)
       set value of text field 1 of sheet 1 of window 1 to "my_saved_screenshot"
   end tell
   key code 36
end tell

What is going on here ? It seems that the variable `sheet 1 of window 1` does not have the same value in the two situations.

Model: MacBook Air
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14

Offline

 

#2 2020-11-21 04:36:45 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 481

Re: Code snippet works in isolation but not when embedded(fill text field)

I think you need a ex. 'delay 1' between the keystroke in the first Script.
Sometimes the only way to make GUI Scripting to work is to make a fast computer to be slower.

Your second Script do only use 1 keystroke so it has time to execute every line in the code.


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#3 2020-11-21 11:53:05 pm

cocoa_on_apple
Member
Registered: 2020-11-20
Posts: 4

Re: Code snippet works in isolation but not when embedded(fill text field)

Fredrik71 wrote:

I think you need a ex. 'delay 1' between the keystroke in the first Script.
Sometimes the only way to make GUI Scripting to work is to make a fast computer to be slower.

Your second Script do only use 1 keystroke so it has time to execute every line in the code.



Worked for me, thanks.
Since I intend to eventually put this in a loop, I'm tempted to use smaller values with delay ... but from what I read, using delay with small fractions of a second is unsafe and not recommended

Offline

 

#4 2020-11-22 07:05:56 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 681

Re: Code snippet works in isolation but not when embedded(fill text field)

cocoa_on_apple wrote:

Since I intend to eventually put this in a loop, I'm tempted to use smaller values with delay ... but from what I read, using delay with small fractions of a second is unsafe and not recommended



Fractional delays seem to work fine in most circumstances. The following is from the discussion of the delay command in the AppleScript Language Guide:

number: The number of seconds to delay. The number may be fractional, such as 0.5 to delay half a second....

delay does not make any guarantees about the actual length of the delay, and it cannot be more precise than 1/60th of a second. delay is not suitable for real-time tasks such as audio-video synchronization.



https://developer.apple.com/library/arc … mentID_714

Last edited by peavine (2020-11-22 08:52:03 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)