Saturday, July 2, 2022

#1 2022-03-29 03:23:10 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 545

Different result if code is run directly using "run script"

This code:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Script Debugger"
   set activeDoc to make new document with properties {name:"Test Save Script"}
   set source text of activeDoc to "display dialog \"I ran!\""
   set docName to the name of activeDoc
   set savePath to ((POSIX path of (path to downloads folder)) as text) & "Test Save.app"
   set exportFormat to enhanced application
   save activeDoc in POSIX file savePath as exportFormat with run only
end tell
tell application "System Events"
   tell application process "Script Debugger"
       tell window docName
           if exists sheet 1 then
               tell sheet 1
                   repeat 200 times
                       set buttonProps to (properties of button "OK")
                       if enabled of buttonProps is true then
                           click button "OK"
                           exit repeat
                       else
                           delay 0.1
                       end if
                   end repeat
               end tell
           end if
       end tell
   end tell
end tell
tell application "Script Debugger" to close activeDoc saving no

Has the result of saving an app in the user's downloads folder that, if run, displays a dialog "I Ran."

This script, which is identical to the above script, except run inside a "run script:"

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set scriptText to "use AppleScript version \"2.4\" -- Yosemite (10.10) or later
use scripting additions

tell application \"Script Debugger\"
   set activeDoc to make new document with properties {name:\"Test Save Script\"}
   set source text of activeDoc to \"display dialog \\\"
I ran!\\\"\"
   set docName to the name of activeDoc
   set savePath to ((POSIX path of (path to downloads folder)) as text) & \"TestSave.app\"
   set exportFormat to enhanced application
   save activeDoc in POSIX file savePath as exportFormat with run only
end tell
tell application \"System Events\"
   tell application process \"Script Debugger\"
       tell window docName
           if exists sheet 1 then
               tell sheet 1
                   repeat 200 times
                       set buttonProps to (properties of button \"OK\")
                       if enabled of buttonProps is true then
                           click button \"OK\"
                           exit repeat
                       else
                           delay 0.1
                       end if
                   end repeat
               end tell
           end if
       end tell
   end tell
end tell
tell application \"Script Debugger\" to close activeDoc saving no"


run script scriptText

immediately returns the error:

Error: Script Debugger got an error: AppleEvent timed out. (-1712)



I discovered this when trying to get Script Debugger to batch save scripts... trying to avoid the UI dialog sheet following the save, which I'm bypassing with UI scripting above. So I'd still be interested in bypassing that, if possible. There's a thread on that at Late Night Software's Script Debugger forum here: https://forum.latenightsw.com/t/cant-ge … -on/3678/2

I wasn't aiming to cross-post that, but it's turned into two questions:
1. The very Script-Debugger-centric question "how can I avoid the dialog on save/export?"
2. The question I assume doesn't relate to script debugger in particular, "Under what circumstances does the same code behave differently when run as a "do script," as opposed to being run directly?

I thought it was better to put question #2 here.


Ditched the Hackintosh.
Intel Mac Mini i7 and M1 Mac Mini on a KVM.


Filed under: "run script"

Offline

 

#2 2022-03-29 10:27:29 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2351

Re: Different result if code is run directly using "run script"

hello,

Without the first 2 lines of code, which are redundant, your 2nd script runs successfully for me.

As for the answer to your first question,, simply close front document saving no:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Script Debugger"
   set activeDoc to make new document with properties {name:"Test Save Script"}
   set source text of activeDoc to "display dialog \"I ran!\""
   set docName to the name of activeDoc
   set savePath to ((POSIX path of (path to downloads folder)) as text) & "Test Save.app"
   set exportFormat to enhanced application
   save activeDoc in POSIX file savePath as exportFormat with run only
   close document 1 saving no -- ADDED
end tell

Last edited by KniazidisR (2022-03-29 10:53:08 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2022-03-30 08:17:21 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 545

Re: Different result if code is run directly using "run script"

That's interesting... so without the first two lines of code, you mean:

Applescript:

set scriptText to "use AppleScript version \"2.4\" -- Yosemite (10.10) or later
use scripting additions

tell application \"Script Debugger\"
   set activeDoc to make new document with properties {name:\"Test Save Script\"}
   set source text of activeDoc to \"display dialog \\\"
I ran!\\\"\"
   set docName to the name of activeDoc
   set savePath to ((POSIX path of (path to downloads folder)) as text) & \"TestSave.app\"
   set exportFormat to enhanced application
   save activeDoc in POSIX file savePath as exportFormat with run only
end tell
tell application \"System Events\"
   tell application process \"Script Debugger\"
       tell window docName
           if exists sheet 1 then
               tell sheet 1
                   repeat 200 times
                       set buttonProps to (properties of button \"OK\")
                       if enabled of buttonProps is true then
                           click button \"OK\"
                           exit repeat
                       else
                           delay 0.1
                       end if
                   end repeat
               end tell
           end if
       end tell
   end tell
end tell
tell application \"Script Debugger\" to close activeDoc saving no"


run script scriptText

This still fails for me, with the "Error: Script Debugger got an error: AppleEvent timed out. (-1712)" error.

This may be because I'm on the Beta version of Script Debugger - Shane said a bug had been introduced that he'll fix in the next beta.

However, your suggestion about "close saving no" right after the "save" does work... I didn't actually have to use UI scripting to dismiss the dialog, the document does still respond to the "close' command even though it has an attached sheet/dialog that needs to be dismissed. So that is a better solution there.

I'm still curious what behaves differently between "run script" and running the same code directly, that I get different results. It's not critical, I've got a good work around. I'd just like to understand what's going on with Applescript. In what other circumstances might I see different behavior running the same code in different ways?


Ditched the Hackintosh.
Intel Mac Mini i7 and M1 Mac Mini on a KVM.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)