After Effects (Mac) Execute a Script from Terminal - macos

I'm trying to figure out how to execute an After Effects script from the command line.
Official documentation says:
Example (for Windows):
afterfx -r c:\script_path\example_script.jsx
And nothing for Mac. However I'm trying:
open -b com.adobe.AfterEffects --args -r /script_path/example_script.jsx
And nothing happens. The program get opened though, but it seems that the script is never called.
Anyone had figured out this?

This is actually documented here
You should now be able to use DoScriptFile (instead of DoScript) with Applescript. Something like:
tell application "Adobe After Effects CS6"
DoScriptFile path/to/file.jsx
end tell
you can also call a specific function using external parameters by mixing this method with the one mentioned by MentalFish
make an applescript [in this case called ASfile.scpt]:
on run argv
set SomeName to (POSIX file (item 1 of argv))
tell application "Adobe After Effects CS6"
DoScriptFile SomeName
DoScript item 2 of argv
end tell
end run
from python and assuming you have a 64bit machine you can call:
ae = subprocess.call('arch -x86_64 osascript /AppleScriptLocation/ASfile.scpt %s/SomeScript.jsx "SomeFunction(\'%s\')"' %( ScriptsFolder, ParameterFromPython),shell=True)

Apparently AE on Mac does not support command line execution of scripts: http://www.aenhancers.com/viewtopic.php?f=8&t=1903
This works, create an AppleScript that tells AE to run the script via the DoScript command:
set test to (POSIX file ("/Users/username/Desktop/test.jsx"))
tell application "Adobe After Effects CSYourVersionNumber"
DoScript test
end tell
... and run the script via the command line as such (to run it in 32-bit mode):
arch -i386 osascript test.applescript
If you want to pass in the parameter of what script to launch you can do it like this:
on run argv
set test to (POSIX file (item 1 of argv))
tell application "Adobe After Effects CSYourVersionNumber"
DoScript test
end tell
end run
Run the script and pass in the path to the jsx file:
arch -i386 osascript test.applescript /Users/username/Desktop/test.jsx

you could place it in the startup folder (I use windows, the folder in win is Adobe After Effects CS4/Support Files/Scripts/Startup). Scripts in that folder are executed upon AfterEffects booting up.
Might be helpful?

I would like to throw in here, some answers where great though possibly outdated.
This code:
on run argv
tell application "Adobe After Effects CC 2018"
DoScriptFile item 1 of argv
end tell
end run
For the applescript file works, the POSIX code does not.
Then running it in the terminal for Mojave works like this:
osascript AppleScriptAETest.scpt "/Users/.../Your/file/here.jsx"
This works best for me.

I got it working with this:
open -b com.adobe.AfterEffects --args /Users/[user]/Desktop/[folder]/file.aep
seems all I had to do was get rid of the 'r' flag.
Good luck!

Related

OSX: How can I programatically tell if I'm running in Terminal or iTerm?

I have a command line application running on OSX that would like to create several tabs in the current window using AppleScript.
How can I tell if my program is running in Terminal, iTerm, or another terminal program?
The $TERM_PROGRAM env var is set to iTerm.app or Apple_Terminal so you can determine which AppleScript cmds to run if you pass it as an arg to osascript (assuming your shelling to osascript)
Example usage:
osascript ThisScriptName.scpt $TERM_PROGRAM
Example script:
--osascript ThisScriptName.scpt $TERM_PROGRAM
on run {TermType}
if (TermType = "iTerm.app") then
-- iTerm.app :-P
tell application "iTerm"
tell current window
tell current session
set newSession to (split horizontally with default profile)
end tell
end tell
end tell
else if (TermType = "Apple_Terminal") then
-- Terminal.app
tell application "Terminal"
do script "open 'https://iterm2.com/downloads.html'" in window 1
end tell
else
-- Unknown terminal application
return "Really? Not using iTerm.app or Terminal.app"
end if
end run
osascript -e 'tell application "Finder" to get the name of every process whose visible is true'
This will a list of running applications, assuming your only running one of terminal, iTerm, ..., this will work
Do what you want with the list
Hope this helps

Use one terminal instance for many do shell scripts?

So, I need to make an AppleScript to ssh into my mint headless server. I don't need to transfer files, just run commands. The issue is, when I ssh with one do shell script, it is in a separate instance than the others. Also, bonus if I don't have to use keystrokes and keep the terminal in front.
Edit: It would help if you guys told me why you're downvoting this post.
Not sure if that is what you want but with Terminal.app you can do that:
set shellScript to "echo" & space & quoted form of "Test Message"
tell application "Terminal"
activate
try
if (exists tab 1 of window 1) then
do script with command shellScript in front window
else
do script with command shellScript
end if
on error
beep
end try
end tell
Above code executes shellScript in the front window or in a new window when no window is open. Hope it helps.
ADDITION
I think there is no AppleScript script command to hide an application.
With System Events we can do a keystroke, so let's do cmd-h which hides an application:
tell application "System Events"
tell application process "Terminal" to set frontmost to true
-- delay 0.25 -- in case it does not work
keystroke "h" using command down
end tell
I don't know how Terminal.app will behave with hiding and doing scripts but you will find out.
Or put the Terminal-Window in another Finder Space.

AppleScript: execution error -10810 when launching certain applications from shebang'ed scripts

I'm running OS X 10.10.2. I'm facing a weird issue where AppleScript won't launch applications from shebang'ed scripts while working fine everywhere else (Script Editor, piping to osascript, etc.). Specifically, consider the following example script named launch-app:
#!/usr/bin/osascript
launch application "TextEdit"
When TextEdit is not running and I do
./launch-app
I get
./launch-app:0:29: execution error: An error of type -10810 has occurred. (-10810)
When I do
<launch-app osascript
Well, it works just fine; which means the following Bash script will also work:
#!/usr/bin/env bash
osascript <<EOF
launch application "TextEdit"
EOF
Really weird. (By the way, a tell ... activate ... end tell block results in the same error. I'm using launch here just to keep to example minimal.)
I have some old scripts that involve activating an application (well, practically all my old scripts involve tell ... activate ... end tell) that definitely worked in the past. I can't tell when things began to fall apart because when I run those scripts, most often the applications to activate are already launched. I have the impression that the issue dates back at least to 10.10.1.
I have looked at several related posts here on SO, e.g., this one, but they don't help. I also tried to understand error -10810 by reading articles like this one, but my problem definitely doesn't look like a filled process table (otherwise why does directly calling osascript works while running osascript from a shebang doesn't?).
Update: The bug has been fixed in OSX 10.10.3.
Just to provide a state-of-the-union post:
The behavior observed is a bug in OSX 10.10 still unresolved as of OSX 10.10.2 (as of 10 Mar 2015):
Anyone interested in getting this fixed should file their own bug at http://bugreport.apple.com.
It applies to executable scripts that are directly or indirectly passed to osascript - whether:
explicitly (osascript launch-app)
or implicitly, via the shebang line, by direct invocation (./launch-app)
The specific form of the shebang line is irrelevant (whether #!/usr/bin/osascript or #!/usr/bin/env osascript or #!/usr/bin/env osascript -l JavaScript or ...), what matters is whether the file has the executable bit (permission) set (e.g., via chmod +x).
Workarounds:
As suggested by the OP, feed the file to osascript via stdin: osascript < launch-app
This has side effects; for instance, name of me will report msng instead of the name of the script.
Remove the executable bit from the script and invoke it explicitly with osascript:
chmod -x launch-app # a one-time operation
osascript launch-app # with the executable bit unset, this should work
Looking at the man page for osascript, when you send lines of applescript code you should put the "-e" option infront of each separate line.
So here's what I tested. I made a bash script with the -e option...
#!/bin/bash
osascript -e 'launch application "TextEdit"'
And one without.
#!/bin/bash
osascript 'launch application "TextEdit"'
The one without the -e option does not run. As such I think this could be a cause of your problem... there's no -e option in your code.
Note that I tested your code too and got the same error as you. There's a command line utility "/usr/bin/macerror" and I entered your error code into that. Here's the result.
Unknown error (-10810) at /usr/bin/macerror5.18 line 40, <DATA> line 1.
Good luck.
There is no need for using osascript to launch applications. There is a built in command line utility named open, that will open your app from the terminal commandline, or a shebanged script. For doucumentation, enter "man open" in a terminal window. It is a really nifty utility, with a lot of options. :)
The open utility, will lauch applications that are not running, but I also wonder out of curiosity: have you tried "tell application appname to run", or just "tell application appname to activate"?
The osascript below, works for me, on 10.9
#!/usr/bin/osascript
tell application "TextEdit" to launch
I guess you'll have to commmand the app to do something, and not just try to "launch" it. Maybe "tell me to launch application appname also works".
Edit
I prefer to use open -b "com.apple.textedit", because then I also get the front window of textEdit, brought to front.
By the way, with the open -e command, you can open a textfile directly into TextEdit from the commandline. open is not totally as good as the plumb utility of plan-9, but it is really nifty.

Mac: gnome-terminal equivalent for shell script

I am trying to run a shell script in a MAC terminal. I want to open a new terminal window that will execute the script separate from the program that is running. In Fedora, there is a gnome-terminal command which lets me execute a script in another terminal shell.
Does anyone know an equivalent on MAX OSX and how to use it?
For example say I have a script crazy.sh and I want to call this from a program that is executing but in a separate terminal from the one which is currently executing the program.
I like DigitalTrauma's answer but I found for my use, this worked better
open -a Terminal.app crazy.sh
Thanks for the answers.
One way to do it is to use an xterm instead of a terminal window:
xterm -e crazy.sh
If you want the xterm to stay open after the script completes, use the -hold option to xterm.
But if you really need to do this in a terminal, you can do it with applescript:
tell application "Terminal"
activate
tell application "System Events" to keystroke "n" using command down
repeat while contents of selected tab of window 1 starts with linefeed
delay 0.1
end repeat
do script "crazy.sh" in window 1 -- make sure the path to your script is right
end tell
(Credit to the answer here https://superuser.com/questions/466619/open-new-terminal-tab-and-execute-script)

open programs with applescript

2 part question:
I'm simply trying to run programs using applescript from the terminal, so I tried:
$ osascript tell application "iTunes" to activate
and get the error:
osascript: tell: No such file or directory
Giving the full path to the program did not work either. What am I missing? The second part of the question is what I eventually want to use applescript for. I would like to use it to open an application I built using py2app. Can applescript open any mac app or just certain ones that are already compatible.
Thanks
Try this. Notice you use "-e" when you are writing the command. Without "-e" you would give a path to an applescript to run. Also notice the string command must be in quotes.
osascript -e "tell application \"iTunes\" to activate"
And if you have a multi-line applescript you use "-e" before each line like this...
osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell"
If you want to open an application just use the unix "open" command...
open "/path/to/application"
If you wanted to open an application using applescript and the "activate" command doesn't work (it should work for almost everything though) then tell the Finder to open it. Remember that applescript uses colon delimited paths...
osascript -e "tell application \"Finder\" to open file \"path:to:application\""
In a bash shell (like in Terminal), you can send multiple lines to osascript by using a "here document".
osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell"
becomes
osascript <<EOF
tell application "iTunes"
activate
end tell
EOF
As an old-skool Unix hacker, I save these little snippets in my $HOME/bin directory and call them from the command line. Still learning the particulars, though.
Alan
an alternative to osascript:
open -a Calendar
close by:
pkill Calendar
Try:
do shell script "open /Applications/iTunes.app"
you need to put single quotes around the tell:
osascript -e 'tell app "iTunes" to activate'
otherwise you're defining a variable when you run -e
I'am new to script too.
I am confused to so I scan an essay named AppleScript Language Guide
and when I go through script commands items, I learn that if you want to activate an application in mac os with applescript editor you should type beneath code in your editor and then compile and run them! may this answer will help you, here's code:
// applescript editor code
----------
activate application "iTunes" line 1
----------
tell application "iTunes" to activate line 2

Resources