1

How can I import a global variable from one AppleScript file into another?

I am using two AppleScript files to create a demo for a project course.

One AppleScript file "main.scpt" begins with a global variable

global someDirectory
set someDirectory to "~/Documents/cs123-drj/demo"

on openServerWindow()
    # Open the server
    tell application "System Events" to keystroke "n" using command down
    tell application "System Events" to keystroke "i" using {command down, shift down}
    typeKeys("server")
    typeKeys(return)
    tell application "System Events" to keystroke "i" using command down
    typeKeys("cd ")
    typeKeys(someDirectory)
    typeKeys(return)
    typeKeys("./cs123-server.sh")
    typeKeys(return)
end openServerWindow

This works fine when executed from this file. I would like to use this file as a library, in a similar to fashion to what is found here. The full text of my second AppleScript follows.

#
# Demo script for doing simultaneous selects from a CS123-DRJ database.
#

property CS123Commands : load script POSIX file "/Users/admin/Documents/cs123-drj/demo/main.scpt"

tell CS123Commands to openServerWindow()

When I attempt to run this code, I get the following error:

error "The variable someDirectory is not defined." number -2753 from "someDirectory"

How can I import this variable into my second AppleScript file?

Community
  • 1
  • 1
Robert Karl
  • 7,598
  • 6
  • 38
  • 61

1 Answers1

4

You are not actually running the script when you load it so someDirectory never gets set. You can fix this by making it a property instead. So change this...

global someDirectory
set someDirectory to "~/Documents/cs123-drj/demo"

to...

property someDirectory: "~/Documents/cs123-drj/demo"
regulus6633
  • 18,848
  • 5
  • 41
  • 49