Is it possible to use global vars from an imported script?

Tried this:

TextUtil has this global defined:

global cr
set cr to ASCII character 13

I import TextUtil into a separate script:

property TextUtil : script "TextUtil"

Then attempt to declare ‘cr’ as property:

property cr: my TextUtil's cr

And get the error:

Can't make cr into type reference

Is this possible to do, or even worth it? (code ‘my TextUtil’s cr’ seems cumbersome). Seems like it might be easier just to declare these globals in each script I need them.

because cr is a global, its initial value is “missing value”. It hasn’t been set yet.
the line “set cr to ASCII character 13” doesn’t get executed unless you tell the script to run.
like so

tell my TextUtil to run

only after will cr be holding a value

or you can instead make cr a property in script “TextUtil” like so…

property cr : return -- same as ASCII character 13
1 Like

Great, thanks.

Using property seems simpler and works fine.

Was trying to use your 1st example of
tell my “scriptname” to run

And couldn’t get it to work.

I have a library script called MSUtil, and added some code to set the current User’s MS Templates folder.

set templatesPath to ""
tell application "Microsoft Word"
	activate
	set templatesPath to (get default file path file path type user templates path) as string
end tell

If I attempt to access templatesPath from a calling script, like so:

property MSUtil : script "MSUtilities"
tell my MSUtil to run

display dialog ("templatesPath:" & my MSUtil's templatesPath)

I get an error saying it can’t be displayed as unicode text. But, it’s clear that the ‘tell my MSUtil to run’ line does not cause the script to run.

Am I missing something?

Is templatesPath set as a global?
It won’t work with local variables

Sample script

on run
	script V
		global templatesPath
		on run
			tell application "Microsoft Word"
				activate
				set templatesPath to (get default file path file path type user templates path) as string
			end tell
		end run
	end script
	
	tell V to run
	get V's templatesPath
end run

I did use a global, but was unable to retrieve the global from calling script.

Instead, I created a sub within MSUtil to return the path:

to getMSTemplatesPath()
	
	set msTemplatesPath to ""
	tell application "Microsoft Word"
		activate
		set msTemplatesPath to (get default file path file path type user templates path) as string
	end tell
	
	return msTemplatesPath
	
end getMSTemplatesPath

No need for the first line

set msTemplatesPath to “”

1 Like