Compton Posted September 11, 2013 Posted September 11, 2013 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
jdfwarrior Posted September 11, 2013 Posted September 11, 2013 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.
Compton Posted September 11, 2013 Author Posted September 11, 2013 (edited) 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 September 11, 2013 by Compton
Compton Posted September 11, 2013 Author Posted September 11, 2013 (edited) 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 September 11, 2013 by Compton
rice.shawn Posted September 11, 2013 Posted September 11, 2013 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.
rice.shawn Posted September 11, 2013 Posted September 11, 2013 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
Compton Posted September 11, 2013 Author Posted September 11, 2013 (edited) 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 September 11, 2013 by Compton
rice.shawn Posted September 12, 2013 Posted September 12, 2013 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.
Compton Posted September 12, 2013 Author Posted September 12, 2013 (edited) 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 September 12, 2013 by Compton
rice.shawn Posted September 13, 2013 Posted September 13, 2013 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.
rice.shawn Posted September 13, 2013 Posted September 13, 2013 Here you go: https://github.com/shawnrice/one-off-experiments/raw/master/tt.alfredworkflow
Compton Posted September 13, 2013 Author Posted September 13, 2013 Here you go: https://github.com/shawnrice/one-off-experiments/raw/master/tt.alfredworkflow 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now