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?
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"
Related
I'm trying to get AppleScript to select a file, but I'm getting an error when I execute the script.
Here's the code
tell application "System Events"
set a to "/Users/me/files/"
set fileName to "myFile.jpg"
set thePath to POSIX path of a
tell application "Finder"
set selection to fileName of thePath
end tell
keystroke "c" using command down
end tell
I'm getting an error "Can’t get POSIX path of "/Users/me/files/"
Essentially, what I'm trying to do is find a way to select a file so that I can copy it for later. But I want to copy the actual file, not the path of the file. The idea is to create a service that copies the file so that I can paste it into another application easily.
If there's a better way to do this, then please let me know
These following 2 lines of code would copy your file to the clipboard. This would only work with a single file. Not multiple items.
activate
set the clipboard to POSIX file (POSIX path of (choose file))
I was trying to use a script to open CS:GO with an AppleScript program, but I get the error message:
Handler cannot handle objects of this class -10010
Here's the script:
set myfilepath to "Macintosh HD:Users:Kasper:Desktop:Counter-Strike Global Offensive"
tell application "Finder" to open myfilepath
delay 5
tell application "System Events" to set unixID to unix id of process "csgo_osx"
do shell script ("renice -20 -p " & unixID) password "2306" with administrator privileges
Any ideas why this happens?
Your second line is telling Finder's Open command to open a string. It needs to be given a file or alias. Change that line to:
tell application "Finder" to open file myfilepath
What would be a simplest way to change a current working directory (cwd) using AppleScript. It would be equivalent to OS's cd or Python's os.chdir. Would be great if a destination directory if doesn't exist would be created on a fly (but that would be highly optional).
If you are looking to use with an applications save dialog...
set filePath to "path/to/my/file"
tell application "System Events"
-- Once save dialog is open
keystroke "g" using {shift down, command down}
delay 1
set value of text field 1 of sheet 1 of sheet 1 of window 1 to filePath
end tell
You might be thinking of doing something like this:
tell application "Finder"
# ^^^ or whatever application you want to control up there
# to get to a file...
set filePath to POSIX file "/Users/username/Documents/new.mp3"
# to get to a folder...
set testFolder to POSIX path "/Users/username/test"
if (exists (folder testFolder)) then
say "folder exists"
else
make new folder at testFolder
endif
end tell
I'm basing my answer off the answers on this page and this related question (or this one).
If the current line in the frontmost Terminal tab contained the text aa, this would run the command aauptime:
set cmd to "uptime"
tell application "Terminal"
try
do script cmd in window 1
on error
do script cmd
end try
activate
end tell
You could obviously add something like tell app "System Events" to keystroke "ku" using control down before do script, but does anyone know any better solutions?
You know you can call a shell script without targeting the terminal, right?
I am trying to write some automation code (primarily in Ruby Selenium). At some point, a file chooser is opened in Safari so that the user can select a file for upload. Selenium cannot handle this, but I think AppleScript should be able to. I am new to AppleScript and haven't been able to find any boilerplate code of someone automating a file chooser dialog. I'm reading through the AppleScript docs, but any ideas would be most helpful.
Some more searching and I found a great answer here: Applescript file dialog with UI scripting
Here's what I ended up using:
on run argv
tell application "Safari"
activate
-- Usage check
set argc to count argv
if argc is not greater than 0 then
return "Usage: SafariFileChooser file_name [window_name]"
end if
-- The file we will choose to open
set file_name to item 1 of argv
-- Flip to the named window, if specified
if argc is equal to 2 then
set window_name to item 2 of argv
set flip_count to index of window window_name
repeat (flip_count - 1) times
activate
tell application "System Events" to keystroke "`" using command down
end repeat
end if
-- Interact with the dialog using System Events (thanks mcgrailm)
tell front window
activate
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke file_name
delay 1
keystroke return
delay 1
keystroke return
end tell
end tell
end tell
return 0
end run
Another option I just discovered is to specify the directory using the command-line:
do shell script "defaults write com.apple.Safari NSNavLastRootDirectory /path/to/directory"
This way you can do slightly less in UI scripting. Run this command before you open the file chooser, and it will put you into the directory specified. Include all the files you need in this directory, and you can just script command+a to select them all, and return.