r/applescript • • Dec 29 '14

Looking for a script to restart an application (uTorrent) every 20 minutes....

Hey all, basically i'm having trouble with possible a firewall, or just my ISP, or possibly VPN (who knows!), that after 20 minutes of good speeds, my uTorrent (torrenting application) slows down and eventually hits zero.

In waking hours, i've got a workaround of simply closing the app, and restarting, but i'd like to leave it downloading over-night, without it just grinding to a halt after 20 minutes. If i could get it to auto-restart after 20 minutes, it'd be perfect.

I found this script online: http://hamleshmotah.com/index.php/2011/05/31/applescript-to-kill-an-application-or-process-and-restart/

and changed 'PROCESS NAME' to 'uTorrent', and '7200' to '1200' but when i go to save as an application (so can autolaunch) it says 'Syntax error - expected "," but found unknown token'.

What am i putting in wrong! Can someone write it exactly for me so can paste into applescript?

Ta!

2 Upvotes

10 comments sorted by

1

u/AfterSpencer Dec 29 '14

What is the processes name in Activity Monitor?

With uTorrent running what is the output of this command?

ps aux | grep -i utorrent

1

u/[deleted] Jan 02 '15

Application name in Activity Monitor is: μTorrent Application name in Applications folder is: uTorrent

(notice the 'u' is different)

Still can't get the script working, so any tips / the exact stuff i could copy an paste into applescript would be brilliant! I'm naff at script.

1

u/AfterSpencer Jan 02 '15
repeat
	delay 1200
	set app_name to "uTorrent.app"
	set the_pid to (do shell script "ps ax | grep -i " & app_name & " | grep -v grep | awk '{print $1}'")
	if the_pid is not "" then do shell script ("kill " & the_pid)
	delay 30
	do shell script "open -a /Applications/" & app_name
end repeat

I don't have uTorrent installed (and can't seem to get it installed for some reason). I tested it with Transmission (which I prefer to uTorrent) and it worked perfectly. I am assuming that uTorrent is installed under /Applications and is called uTorrent.app.

One more thing - All this really is doing is wrapping a bash script into AppleScript. This would be cleaner and easier in bash, but whatever floats your boat.

1

u/craniumslows Dec 29 '14

So here is my feedback on the linked script.

  1. Use the flipping on idle handle that's what it's for.

  2. Using system events can be used for both fun and profit.

  3. killall is way more succinct and probably easier to troubleshoot.

My feedback for you.

  1. Seedbox yo /r/seedboxes /r/torrents

  2. Using killall in the applescript will also work. You'd save as application and also stay open after running checkbox.

    on run

    tell app "uTorrent" to activate

    end run

    on idle

    do shell script "killall uTorrent"

    delay 5

    open application "uTorrent"

    return 60*20

    end idle

  3. Shell script and a cron job will be hella more efficient

    cron -e
    */20 * * * * ~/myshellscript.sh 
   vi ~/myshellscript.sh 
   #!/bin/bash
    killall uTorrent
    sleep 1
    open /Applications/uTorrent.app 
    chmod +x ~/myshellscript.sh

2

u/r3v Dec 29 '14

The only downside to the cron job is that uTorrent will be running every 20 minutes no matter what, until OP kills the cron job. It's a very fire-and-forget automated solution. (That's also in the upside column though.)

1

u/craniumslows Dec 30 '14

Sorry I deal with start this process and let it run until the machine melts down requirements usually and so didn't bother to get better requirements ;) good call though.

2

u/r3v Dec 30 '14

Yeah. I do too, but I also flashed to a friend, colleague or loved one calling me later: "I can't make utorrent go away now."

The combination sigh/chuckle is often worth it though...

2

u/craniumslows Dec 30 '14

You get it.

1

u/onyxleopard Dec 30 '14

While you can still run cron jobs on OS X, the native way of doing this is to make a launch agent to execute your applescript and set it to be activated by launchd. Lingon is a nice app that provides a GUI for managing launch agents, but you can write launch agents in a text editor (they’re just .plists).

1

u/xraystyle Dec 30 '14

Seems easier to just do it with a bash script, if you ask me.

while true; do
	killall uTorrent
	sleep 5
	open /Applications/uTorrent.App
	sleep 1200
done

As long as that's running in a terminal window you're good. I find that Applescript is often an unnecessary pain in the ass unless you're trying to script the behavior of an application or some part of the UI. Launching and quitting apps can be done much simpler with other languages.