Struggling with this bit too long, trying my luck here.
I'm running a simple task with applescript which is wrapped inside condition:
If application iTerm2 is running.
If the iterm2 is running, then everything is fine and the execution is successful but if iTerm2 is closed then I'm getting the following syntax error:
132:138: syntax error: Expected end of line but found class name.
(-2741)
If it matters I'm, running it via Alfred
Here's the script
on run
if application "iTerm2" is running then
tell application "iTerm2"
tell current window
create tab with default profile
end tell
end tell
end if
end run
Any help is much appreciated, thanks
why do you need the if clause? Try this and it should start iTerm2 if it's not running:
on run
tell application "iTerm2"
activate
tell current window
create tab with default profile
end tell
end tell
end run
Related
For example in the tutorial here
tell application "Finder"
Finder is an app in my system.
Terminal as well.
However, if I replace Terminal with iTerm which is also an App named like that in my applications list, it wouldn't work. Even it is shown in some of the answer from SO like following
#!/bin/bash
osascript -e '
tell app "iTerm"
activate
tell the first terminal
launch session "Default Session"
tell the last session
set name to "New Session"
write text "cd /usr/bin; ls"
end tell
end tell
end tell'
it will return error of
52:60: syntax error: Expected class name but found identifier. (-2741)
Or something like
#!/bin/bash
osascript -e '
tell application "Terminal"
do script "date"
activate
end tell'
It works, but when I replace Terminal to iTerm it returns error of 35:41: syntax error: Expected end of line but found “script”. (-2741)
So suspect I am using the wrong app name even it appears to be iTerm is the actual app name, but it seems not, as it caused error above. If I can have a list of the actual app names I can use, it may help me to tackle it
*NOTE: I do have iTerm installed, and that's why I want to refer to it...
My applescript gave correct output when ran inside automator as service with no input but it gives error message when ran outside automator (safari, preview etc.)
It also gives correct output when ran through applescript editor, no problem running it with safari, preview or any other application.
The reason I was so keen to get it done with automator was that I don't want to install a 3rd party app just for assigning one shortcut to one script.
I debugged this portion which after removing from code allowed automator to run the script. Ran it with try, error in automator & viola, no issues at all.
But I want to know why this code earlier & without try-error function was giving the error messages when ran outside automator application.
Here's the portion of the script with try-error function:
try
set writ to do shell script "/usr/bin/python Users/[username]/Documents/tech_toolkit/windtitle"
tell application writ
if the (count of windows) is not 0 then
set window_title to name of front window
end if
end tell
on error
delay 2
end try
For shell script /windtitle , windtitle is an executable file
#!/usr/bin/env python
from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print active_app_name
The error is:
Your python script return the "Automator Runner" application when it run as service , it's
the frontmost in the background but not in the foreground.
To get the frontmost application, use this script:
try
tell application (path to frontmost application as text)
set window_title to name of front window
set writ to its name -- get the name of this application
end tell
on error
return -- quit this script, because this application is not scriptable, or no window
end try
I have the following Applescript which runs fine from the applescript editor in both Snow Leopard and Lion. I can also run it using osascript via terminal on Snow Leopard but not on Lion. The code is as follows
tell application "System Events"
try
get process "Mathematica"
on error -- Not running, launch and run
launch application "Mathematica"
-- May need to wait until application finishes launching
repeat while "Mathematica" is not in name of processes
delay 0.5
end repeat
end try
end tell
tell application "Mathematica"
activate
do script "notebook0=NotebookEvaluate[\"/Users/me/file.nb\"];
Clear[notebook0];NotebookClose[EvaluationNotebook[]]"
end tell
The script opens and executes a Mathematica script. It is successful for the scenarios above but when I try to run it using osascript in Lion I get a Mathematica window which states that "the requested file could not be found or accessed".
Help greatly appreciated.
When you launch application "Mathematica", I suggest you provide the full path for your application.
I have been trying to launch two applications that I created with a single applescript file. (I am using OS X Lion)
The code I am using is:
tell application "app1" to activate
tell application "app2" to activate
Doing either one of these two tells works, but both together only launches the first application...
Does anyone know what I an doing wrong?
The first application is a C application compiled by xCode, and the second application is an AIR/Flash application created/published in Flash CS4
Edit
I've also tried
do shell script "my_path_here/app1"
do shell script "my_path2_here/app2"
which only launches the first application.
I've also tried:
do shell script "my_path_here/app1" & "; my_path2_here/app2"
which STILL only launches the first application.
Edit 2
I've discovered that this script will work:
do shell script "cd my_path_here/ ; open app1 ; cd my_path2_here/ ; open app2"
This accomplishes what I need done, but still leaves open the original problem, as this is a workaround and not a fix... This might shed more light on what the issue could be though.
I couldn't reproduce the issue, so can't tell you why it's happening, but (assuming there's a timing problem) introducing a delay between the application launches may resolve the problem. You'll need to experiment with the delay time.
tell application "app1" to activate
delay 10
tell application "app2" to activate
tell application "app1" to activate
end if
tell application "app2" to activate
end if
Try that and see how it works.
I've discovered that this script will work:
do shell script "cd my_path_here/ ; open app1 ; cd my_path2_here/ ; open app2"
This accomplishes what I need done, but still leaves open the original problem, as this is a workaround and not a fix... This might shed more light on what the issue could be though.
This script worked every time with Snow Leopard.
tell application "Terminal"
activate
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 4
do shell script "open -a Firefox http://localhost:3000"
end tell
With Lion I keep getting this error:
error "Terminal got an error: Can’t get window 1." number -1728 from window 1
Thanks.
I'm running Lion. I can execute the following with no errors. I do not get an error in Applescript in any of the following situations 1) the application is not running, 2) the app is running and a window is open, and 3) the app is running and the directory path is not valid. In case 3 the Terminal shows an error but applescript does not.
tell application "Terminal"
activate
do script "cd Development/Images" in front window -- this line highlighted on error
do script "ls -al" in front window
end tell
So your problem is something not related to this actual code.
It seems the problem is not the code but the speed at which the operating system is functioning. As i mentioned before, the code executed just fine in Snow Leopard. To compensate, after the upgrade to Lion, if i add another delay to give Terminal time to finish activating, and increase the delay before opening Firefox, everything works. My laptop is a MacBook Pro with 2.7 GHz Intel Core i7 processor.
tell application "Terminal"
activate
delay 1
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 5
do shell script "open -a Firefox http://localhost:3000"
end tell
If you are just trying to get a script to run once, simply retrying may help. Execution speed is also a function of other demands on the system.