Jump to content
Compton

Same script but different result in Applescript Editor vs Alfred?

Recommended Posts

I wrote a script which worked perfectly in Applescript editor. I then created a workflow with the same exact script and the outcome is very different. The script checks whether an application called "Trasmission" is running and if so, performs certain things. Below is the simplified version of the script.

 

This simplified version is supposed to launch Transmission if its not running. If Transmission is running, the script is supposed to launch TextEdit. This is exactly what happens if the script is run in Applescript Editor. However, if I copy and paste the script into Alfred and run with the "usr/bin/osascript" language, both scripts are run as s if the variable "is_running" is both true and untrue. Could someone please explain what is going on? Thanks.

tell application "System Events"
	if (name of processes) contains "Transmission" then
		set is_running to "true"
	else
		set is_running to "untrue"
	end if
end tell

if is_running = "true" then
	tell application "Transmission" to activate
	tell application "System Events"
		tell process "Transmission"
			click menu item "Pause All" of menu "Transfers" of menu bar 1
		end tell
	end tell
	tell application "Transmission"
		close window 1
	end tell
end if

if is_running = "untrue" then
	tell application "TextEdit" to activate
end if

 

Share this post


Link to post

 

I wrote a script which worked perfectly in Applescript editor. I then created a workflow with the same exact script and the outcome is very different. The script checks whether an application called "Trasmission" is running and if so, performs certain things. Below is the simplified version of the script.

 

This simplified version is supposed to launch Transmission if its not running. If Transmission is running, the script is supposed to launch TextEdit. This is exactly what happens if the script is run in Applescript Editor. However, if I copy and paste the script into Alfred and run with the "usr/bin/osascript" language, both scripts are run as s if the variable "is_running" is both true and untrue. Could someone please explain what is going on? Thanks.

tell application "System Events"
	if (name of processes) contains "Transmission" then
		set is_running to "true"
	else
		set is_running to "untrue"
	end if
end tell

if is_running = "true" then
	tell application "Transmission" to activate
	tell application "System Events"
		tell process "Transmission"
			click menu item "Pause All" of menu "Transfers" of menu bar 1
		end tell
	end tell
	tell application "Transmission"
		close window 1
	end tell
end if

if is_running = "untrue" then
	tell application "TextEdit" to activate
end if

 

 

Did you change anything about the code above before trying it in Alfred?  The reason I ask is, I don't have Transmission on my machine so I modified this slightly an dumbed it down a little more but can't seem to replicate it on my machine. This is what I did..

tell application "System Events"
	if (name of processes) contains "Safari" then
		set is_running to "true"
	else
		set is_running to "untrue"
	end if
end tell

if is_running = "true" then 
	display dialog "It's true"
end if
if is_running = "untrue" then 
	display dialog "It's not true"
end if

The code checks to see if the application is running and then based on that runs through two if's (not an else, or else if to ensure that it should execute both). Even in Alfred, this returns the correct value every time. If it executed both, I should get 2 dialogs and I only get one each time. So, without looking at the full code, I'm not sure how much help I can be.

Share this post


Link to post
The code checks to see if the application is running and then based on that runs through two if's (not an else, or else if to ensure that it should execute both). Even in Alfred, this returns the correct value every time. If it executed both, I should get 2 dialogs and I only get one each time. So, without looking at the full code, I'm not sure how much help I can be.

 

 

If I copy and paste your script in Applescript Editor, I get the message "It's true" or "It's not true". If I paste the same exact script in an Alfred Workflow and run it, I get no message at all.

 

Here's my workflow with two different actions, "troubleshooting1" and "troubleshooting2": https://dl.dropboxusercontent.com/u/345710/Troubleshooting.alfredworkflow

 

The "troubleshooting1" action runs your script. Like I mentioned, nothing happens if I run it through Alfred but I get the messages if I run it through Applescript Editor.

 

The "troubleshooting2" is my script which is supposed to check (for demonstration purposes) whether Safari is active and if it is, it launches TextEdit. If Safari is not active, it is supposed to launch Preview. This is what happens in Applescript Editor. However, both TextEdit and Safari are launched whenever I run the script through Alfred. If I copy and paste the script from Alfred to Applescript Editor, the script again works correctly. 

 

I'm running Alfred 2.0.7 (205) with OSX 10.8.4. 

Edited by Compton

Share this post


Link to post

Just downloaded the linked workflow on my friend's computer and exactly the same thing occurs. If I run the "troubleshooting2" script through alfred while Safari is not running, the computer behaves as if the condition is true and untrue at the same time, i.e. it launches both programs. However, this does not happen if the same exact code is run in Applescript Editor.

tell application "System Events"
	if (name of processes) contains "Safari" then
		set is_running to "true"
	else
		set is_running to "untrue"
	end if
end tell

if is_running = "true" then
	tell application "TextEdit" to activate
end if

if is_running = "untrue" then
	tell application "Preview" to activate
end if
Edited by Compton

Share this post


Link to post

osascript doesn't have the ability to use dialogs and other events like that. I did find a way around that once, however, (and I forgot how), by calling a script file from a bash shell invoking osascript. At least it was something like that.

Share this post


Link to post

Incidentally, the modified code below works just fine in Alfred's osascript --

 

tell application "System Events"
  if (name of processes) contains "Safari" then
    set is_running to true
  else
    set is_running to false
  end if
end tell


if is_running is true then
  tell application "TextEdit" to activate
end if

if is_running is false then
  tell application "Preview" to activate
end if

Share this post


Link to post

 

Incidentally, the modified code below works just fine in Alfred's osascript --

 

tell application "System Events"
  if (name of processes) contains "Safari" then
    set is_running to true
  else
    set is_running to false
  end if
end tell


if is_running is true then
  tell application "TextEdit" to activate
end if

if is_running is false then
  tell application "Preview" to activate
end if

 

 

It works for me too (as it always has) if Safari is running. In that case only TextEdit is activated. However, if Safari is not running, then TextEdit and Preview get launched.

 

I created a blank workflow with input = keyword and action = run script (language osascript) and pasted your code in. Still both programs get launched if Safari is not running. If Safari is running, only TextEdit gets launched.

 

Can you make sure that you have terminated all three programs (Safari, TextEdit & Preview) and then run the script through alfred using a keyword activation? If it still works for you, then I guess I have to give up and believe that the problem is between my ears ;)

Edited by Compton

Share this post


Link to post

This works just fine and dandy for me with all iterations of programs open/closed (although I have to open safari to test the if statement at one point).

 

It works file with the action -> execute script and "osascript" selected. It also works fine with NSApplescript (as long as you put it between the templated alfred run and end alfred tags).

 

Granted, they are launched in the background (is that not what you're looking for?).

 

Further thoughts: I'm running 10.8.4, with Alfred v2.0.7 (205). I don't think that there would be any other possible variables that would matter...

 

Lastly... why, if you're trying to control Transmission would you want to activate TextEdit? I don't get it.

Share this post


Link to post

Lastly... why, if you're trying to control Transmission would you want to activate TextEdit? I don't get it.

 
Frankly I'm not interested in TextEdit. I'm interested in making sure that I'm not sharing torrent files when I'm connected to my university VPN through a tunneling application called Viscosity. The script is supposed to check whether Transmission is active before turning on the VPN connection and if it is, to turn off file sharing. This is what I started with:
 
on is_running(appName)
     tell application "System Events" to (name of processes) contains appName
end is_running

if is_running("Transmission") then
          tell application "Transmission" to activate
          tell application "System Events"
          tell process "Transmission"
          click menu item "Pause All" of menu "Transfers" of menu bar 1
     end tell
     end tell

tell application "Transmission"
close window 1
end tell
     end if

tell application "Viscosity"
disconnectall
delay 1
connect "HY_VPN"
end tell
The problem is the same as in the simplified Safary/TextEdit/Preview script; Transmission gets unnecessarily activated every time the script is launched through Alfred, regardless of whether it is running or not. In other words, the script acts as if the test condition is true when in fact it is false.
 
I guess I have to give up with trying to run the script directly from Alfred. The script works fine if I save it as an application in Applescript Editor and then only launch it through Alfred. 
 
My final request to Shawn Rice is to create a workflow of the Safari/TextEdit/Preview script and share it. I would like to import it to Alfred and see what happens.
 
In any case, thank you very much for trying to help.
Edited by Compton

Share this post


Link to post

I'll post one in a bit. I have a bunch of other stuff to do on a timeline within the next few hours. But if you're interested in dealing with transmission, then why not just use a bash script that controls transmission? It looks like it has a full way to script it integrated (https://trac.transmissionbt.com/wiki/Scripts). Then, if you want, you could automatically invoke an applescript through viscosity that is triggered when it disconnects (poke around: https://www.sparklabs.com/support/running_applescripts_when_conn/). Use the applescript to trigger another transmission script that will start them back up.

 

Then, your Alfred workflow would just invoke that script to shutdown transmission, which could also then execute another script in serial to startup Viscosity.

 

I'll post a workflow later tonight with the abstracted workflow from above.

Share this post


Link to post

 

Your workflow works but I noticed that you used NSApplescript whereas my problematic workflow was done with "run script" action with /usr/bin/osascript. The same script (without the "on alfred" handler) does not function properly with /usr/bin/osascript.

 

Anyway, thanks for the solution and the other suggestions. I'll definitely look into bash scripting. 

 

big-thank-you3.gif

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...