Jump to content

NSApplecript behaves differently in workflows and terminal feature


Recommended Posts

There are already multiple discussions on Alfred-run Applescript sometimes having a different behaviour than on other places, and in many cases, it seems like nothing can be done about it, since Alfred is not doing anything special with the scripts (which means it may be out of reach for the developers to solve). In this case, however, it’s a case of Alfred apparently having a different implementation in different places. It’s seems more of an oddity than a bug, but there’s no “oddities subforum”.

Using the latest builds of both Alfred (2.0.6) and Mountain Lion (10.8.4).

The code in question is

on alfred_script(q)
tell application "System Events" to set isRunning to (name of processes) contains "iTerm"

tell application "iTerm"
	if not isRunning then
		set _term to (make new terminal)
		set _term to the first terminal
		tell _term to set _session to current session
	else
		set _term to the first terminal
		tell _term to set _session to (launch session "Default")
	end if

	tell _session to write text q
	activate
end tell
end alfred_script

What this does is, taking a command as an argument, if iTerm is currently closed, it’ll open a window, and run the command; if it’s open, it’ll open a new tab on the current window, and run the command.

If I set this on Run NSApplescript connected to Keyword, in a workflow, or run it from AppleScript Editor, it runs just as intended. However, if I set this as the Custom code for the Terminal / Shell option on the Features tab, it does not — if iTerm is closed, it opens it and a new tab, running the command on this second tab, and if iTerm is already open, it’ll behave as expected.

Why is it working differently in these two places? What is it about the Terminal / Shell feature that makes it behave differently (and in a suboptimal manner)?

Link to comment

Why is it working differently in these two places? What is it about the Terminal / Shell feature that makes it behave differently (and in a suboptimal manner)?

 

The only difference is that the Terminal / Shell feature uses osascript (same as a Run Script object set to osascript), and not NSAppleScript as the Run AppleScript action and AppleScript Editor use.

 

I have found that iTerm opens a second tab up when opening from scratch regardless of the AppleScript, so you may want to play with that a little more. If you switch this AppleScript to using Terminal.app, do you get the behaviour you expect?

 

[i am moving this to the help and questions subforum as I need to keep the bugs subforum to Alfred bugs only]

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...