Jump to content
vitor

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)?

Share this post


Link to post

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]

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...