Friday, October 22, 2021

#1 2021-09-25 08:31:27 am

Riku
Member
Registered: 2016-05-23
Posts: 56

How does Applescript run an Applescript that runs an Applescript?

I'm a bit confused about the hierarchies regarding 'run script'.
If you just want to go straight to the point, the gist is in the box.


I've been making .app files for years now, and because I didn't know any better, quite a few scripts had references to external dialog icons, sound effects, or script apps that were stored elsewhere on the disk. This of course is an awful habit, because I lose track of which files are being referenced by a script, and if any folder structures change, everything is ruined.

Recently I 'graduated' to copying subscripts in the "Scripts" folder found from the .app package, and learned how to create a Custom folder to the same directory, so I could store all the other resources there and reference to them. Now none of these scripts contain hardcoded paths to references, and it therefore doesn't matter even if I wanted to rename the .app or move it to a different folder.

I had usually just had one .app run a contained .scpt file, but I just built a pretty massive app and ran into a phenomenon I didn't know of. Here's an example of the logic:

If Mother.app has an embedded script, it can run it with run script ScriptPath & "Child.scpt".
But if Child.scpt then tries run script ScriptPath & "Grandchild.scpt", nothing happens.

What is the correct way to build structures like this, without having multiple script apps popping in and out of the dock (as telling to launch .app files would probably do)?



The reason why I would like to keep all these branches of the script as separate files is that the project mainly consists of choose from list dialogs, which easily makes a script swell into proportions that makes it very difficult to work with. Also, several deduction paths in multiple "Children" reference to the same set of "Grandchildren", which will further reference to the same set of "Grand-grandchildren", so it just doesn't make sense to me to copy-paste all that stuff multiple times to the "Child" scripts. It's a lot of work, and if I encounter a bug from such part (which, with something this big is very likely), it's even more work to have to fix the same bug for all those repeats...

Last edited by Riku (2021-09-25 08:32:24 am)

Offline

 

#2 2021-09-25 01:13:51 pm

Riku
Member
Registered: 2016-05-23
Posts: 56

Re: How does Applescript run an Applescript that runs an Applescript?

Okay I got it working eventually, and the problem wasn't what I thought it was.
But what the problem was, I still don't understand.

Could someone explain, what's the functional difference between these two?
Why and when should one choose one or the other?


Applescript:

set TheScriptToRun to alias ((ScriptPath) & "MyScript.scpt")
run script TheScriptToRun

Applescript:

set TheScriptToRun to ((ScriptPath) & "MyScript.scpt")
run script file TheScriptToRun

When I was writing the script, testing it in isolation, the first format was working fine. I hadn't spontaneously used aliases before, but it looked simple enough so I just copied that format when I quickly googled how the syntax for running a script was...
And turns out, that format doesn't work when you have more than one level of "child/parent" relationships in the script series.
But removing alias and adding file made it work.

Offline

 

#3 2021-09-25 01:29:54 pm

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

Re: How does Applescript run an Applescript that runs an Applescript?

You tried concatenate alias (which is special number type, it is one address in the memory, pointer) with string. In the proper script you concatenate HFS path (which is string type) with other string.

Last edited by KniazidisR (2021-09-25 01:34:01 pm)


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

Online

 

#4 2021-09-25 02:49:30 pm

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

Re: How does Applescript run an Applescript that runs an Applescript?

Riku wrote:

Could someone explain, what's the functional difference between these two?
Why and when should one choose one or the other?



The run-script entries in both the AppleScript Language Guide (ASLG) and the AppleScript dictionary state that either aliases or file specifiers can be used. So, functionally there is not (or at least should not be) any difference between them in this particular case. From the AppleScript dictionary:

run script v : Run a specified script or script file
run script script : the script text (or an alias or file reference to a script file) to run
[with parameters list of any] : a list of parameters
[in text] : the scripting component to use; default is the current scripting component
→ any : the result of running the script



The ASLG makes the following recommendation regarding the use of aliases and file specifiers:

The following is the recommended usage for these types:

Use an alias object to refer to existing file system objects.
Use a file object to refer to a file that does not yet exist.
Use a POSIX file specifier if you want to specify the file using a POSIX path.



In the last few versions of macOS, aliases have not always worked as expected, so I tend to use file specifiers. None of the above would explain the exact issue you have encountered, though.

Last edited by peavine (2021-09-25 03:24:18 pm)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#5 2021-09-25 10:32:17 pm

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

Re: How does Applescript run an Applescript that runs an Applescript?

@peavine,

the issue is obvious. I explained it already. It isn't problem of aliases here, but how the AppleScript concatenates operands:

Applescript:


set anAlias to path to desktop folder

-- CORRECT
-- APPLESCRIPT automatically converts anAlias to text (HFS) here,
-- because the left operand "" is string
set concatanatedHFSPath to "" & anAlias & "myDesktopScript.scpt"
--> "HARD_DISK:Users:123:Desktop:myDesktopScript.scpt"

-- WRONG
set concatanatedHFSPath to anAlias & "myDesktopScript.scpt"
--> {alias "HARD_DISK:Users:123:Desktop:", "myDesktopScript.scpt"}

-- CORRECT
-- APPLESCRIPT automatically converts PI to text here,
-- because the left operand "" is string
set concatanatedHFSPath to "" & pi & "myDesktopScript.scpt"
--> "3.14159265359myDesktopScript.scpt"

-- WRONG
set concatanatedHFSPath to pi & "myDesktopScript.scpt"
--> {3.14159265359, "myDesktopScript.scpt"}

Last edited by KniazidisR (2021-09-25 10:38:04 pm)


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

Online

 

#6 2021-09-25 10:47:21 pm

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

Re: How does Applescript run an Applescript that runs an Applescript?

KniazidisR wrote:

@peavine,

the issue is obvious. I explained it already. It isn't problem of aliases here, but how the AppleScript concatenates operands:



KniazidisR. In my post I was responding to a specific question raised by the OP's, which was:

Could someone explain, what's the functional difference between these two?
Why and when should one choose one or the other [i.e. aliases and file references]:



At the end of my post I made clear that the issue he had encountered was not related to the use of aliases or file references.

None of the above would explain the exact issue you have encountered, though.


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#7 2021-09-25 10:56:01 pm

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

Re: How does Applescript run an Applescript that runs an Applescript?

If you want run script with concatenated alias, then following should work. I will repeat, the OP concatenated wrong way, and this was the only issue. The script, of course should be existing.

Applescript:


set anAlias to path to desktop folder
set concatanatedAlias to ("" & anAlias & "myDesktopScript.scpt") as alias
run script TheScriptToRun

NOTE: @peavine correctly pointed out that in some applications of the latest OS, the aliases do not work (for example, in System Events and Image Events). In tell application blocks, it is better to use the file specification with HFS.

Last edited by KniazidisR (2021-09-25 11:32:59 pm)


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

Online

 

#8 2021-09-26 09:04:21 am

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

Re: How does Applescript run an Applescript that runs an Applescript?

Riku wrote:

If Mother.app has an embedded script, it can run it with run script ScriptPath & "Child.scpt".
But if Child.scpt then tries run script ScriptPath & "Grandchild.scpt", nothing happens.



If I understand Riku's post correctly, he wants to place a script within an AppleScript app's resources folder, and he wants that script to call a second script in the same folder. I've never done this before and thought I'd give it a try. The following is a bit kludgey but does work.

Mother.app

Applescript:

set theChildScript to (path to resource "Child.scpt" in directory "Scripts")
run script theChildScript

Child.scpt

Applescript:

set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set theFolder to text 1 thru text item -2 of (path to me as text)
set theGrandchildScript to (theFolder & ":Grandchild.scpt") as alias
set AppleScript's text item delimiters to ATID
run script theGrandchildScript

Grandchild.scpt

Applescript:

display dialog "ok"

The above code aside, it is not clear to me if his variable ScriptPath is an alias, an HFS path, a path-to command that returns an alias? Perhaps Riku could clarify this.

@KniazidisR. I understand that you have already answered Riku's question. This is a topic of interest to me, and I was just hoping for some discussion of how this might best be done.

Last edited by peavine (2021-09-26 11:04:20 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)