AppleScript - Launch Homebrew's apps

Discussion in 'Mac Programming' started by Pimpampoum, Jan 10, 2015.

  1. Pimpampoum, Jan 10, 2015
    Last edited: Jan 10, 2015

    Pimpampoum macrumors newbie

    Joined:
    Jan 10, 2015
    #1
    Hi there,

    I will want to create an AppleScript to launch Homebrew's app.
    The path of Homebrew's apps is "/usr/local/bin/".
    For example, I would like to start with Mediatomb that is already installed.
    I tried to create it, it works but I would like ameliorate it.

    Here the code :
    Code:
    tell application "Terminal"
    do script "/usr/local/bin/mediatomb"
    end tell
    
    I would like to close the first terminal that opens it (that allows to start Mediatomb) and optional launch Safari with Mediatomb web user interface (address IP and port).
    Is this possible ? Any suggestions ?

    Thanks !
     
  2. kryten2, Jan 10, 2015
    Last edited: Jan 10, 2015

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    You can try the do shell script and open location commands or look at the dictionaries of Standard Additions , Terminal and Safari. The URL property in the Safari dictionary might be useful. Depending on your IP and the port you set for Mediatomb it shouldn't be to difficult to do.

    Info : https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW40

    Note : All of this can be done with a little shell script as well.
     
  3. Pimpampoum, Jan 10, 2015
    Last edited: Jan 10, 2015

    Pimpampoum thread starter macrumors newbie

    Joined:
    Jan 10, 2015
    #3
    Thank you kryten2 for your answer, very interesting.
    I have fixed the GUI port in the Mediatomb config file.
    I added this in the code > tell application "Safari" to open location "http://x.x.x.x:pORT/" it works fine, it misses me the gimmick to close the first terminal, because I have not of ideas to do it actually.
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Try :
    Code:
    
    do shell script "/usr/local/bin/mediatomb"
    Just this single statement. Do not put this in a tell application Terminal block!
     
  5. Pimpampoum, Jan 10, 2015
    Last edited: Jan 10, 2015

    Pimpampoum thread starter macrumors newbie

    Joined:
    Jan 10, 2015
    #5
    Not work with "do shell script" without of other statement.

    When I launch the AppleScript with "do script" and tell, I have firstly the terminal that opens (TTY ttys000) and secondly the Mediatomb server that starts in a new Terminal windows (TTY ttys001).
    The first is blank.
     
  6. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    I've been trying to steer you away of using the Terminal application so you don't have to worry about opening or closing windows. You can get what you want by using these two statements :

    Code:
    do shell script "/usr/local/bin/mediatomb"
    open location "http://x.x.x.x:PORT/"
    If you persist on using the Terminal application you can try something like this :

    Code:
    tell application "Terminal"
    	do script "/usr/local/bin/mediatomb"
    	close last window
    end tell
    
    open location "http://x.x.x.x:PORT/"
     
  7. Pimpampoum thread starter macrumors newbie

    Joined:
    Jan 10, 2015
    #7
    Code:
    do shell script "/usr/local/bin/mediatomb"
    open location "http://x.x.x.x:PORT/"
    That, it runs but I do not control service.
    In AppleScript I can not stop it, it runs in loop (I have to kill the process with the command kill PID in the terminal).

    Code:
    tell application "Terminal"
    	do script "/usr/local/bin/mediatomb"
    	close last window
    end tell
    
    open location "http://x.x.x.x:PORT/"
    That, it runs but the statement "close last window" wants to close the window where Mediatomb server is running.
     
  8. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
  9. Pimpampoum thread starter macrumors newbie

    Joined:
    Jan 10, 2015
    #9
    Code:
    tell application "Terminal"
    	do script "/usr/local/bin/mediatomb"
    	close first window
    end tell
    It works just like that !
    We can change the icon from AppleScript ? (to put Mediatomb icon by example).
     
  10. chown33, Jan 11, 2015
    Last edited: Jan 11, 2015

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    The do shell script AppleScript command will wait for the shell commands to terminate. If the shell commands don't terminate, the AppleScript will wait forever.

    Put the mediatomb command into the background, and the shell script will then terminate, and the AppleScript will proceed:
    Code:
    do shell script "/usr/local/bin/mediatomb &"
    EDIT
    I did some reading here:
    http://mediatomb.cc/pages/documentation

    There's a command-line option (--daemon) that tells mediatomb itself to run as a daemon. This should be preferable to having the shell put it in background. So the preferred AppleScript command would be:
    Code:
    do shell script "/usr/local/bin/mediatomb --daemon"
    This is a guess based only on reading the documentation and guessing that the OP wants mediatomb to continue running rather than terminate.

    There are other options that may be useful, such as --pidfile and --user
     
  11. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #11
    Thanks chown33. I read the same thing when I looked up Mediatomb and I was hoping the OP also did this before running it.

    @Pimpampoum

    Info : how-to-change-app-icon-mac
     
  12. Pimpampoum, Jan 12, 2015
    Last edited: Jan 12, 2015

    Pimpampoum thread starter macrumors newbie

    Joined:
    Jan 10, 2015
    #12
    Thank you both !
    My goal is to control Mediatomb such as a service, an app that I launch and I close it if I run it as a daemon it will occupy a place in the RAM memory.
    I did not will use it often.
    It is to difficult explain you what I want, I do not write English well and I am newbie but I want to understand what I'm doing, sorry.

    If I use this "do shell script "/usr/local/bin/mediatomb --daemon"" what will happen ?
    It is also easy to create a daemon with Homebrew's apps and OSX environnement ?
    I will see the Mediatomb daemon into launchctl list command ?
     
  13. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #13
    I understand the difficulties, but it's important that you explain what you want clearly. If we don't know what you want, or what we understand from your writings isn't what you want, then we can't make solutions.

    Read the documentation for MediaTomb. If that doesn't explain things well enough, then try it and see what happens.

    I don't know what will happen; I can only guess based on what I read.
    I don't have MediaTomb, and I'm not going to install it just to run tests.

    Homebrew is not just daemons.

    If a particular Homebrew app is suitable for running as a daemon, then it's usually straightforward to run it as one. The preferred way to do that is by making a per-user LaunchAgent which can then be controlled with the 'launchctl' command-line tool.

    If no one has made the launchd plist and posted it somewhere, then you will be the first. That means you'll have to try things and see what happens (experiment).

    You should also be prepared for experiments to fail. That may mean losing data, so if you don't have a backup strategy, or aren't taking precautions, the worst will eventually happen and you'll lose data.

    You seem to know what 'launchctl' is, but you haven't said anything about how well you understand its operation. It's a powerful tool, but incorrect use can create crashes or security holes. It's not something I recommend for someone who calls himself a "newbie".

    If you're going to make LaunchAgents, you should read and understand the "Daemons and Services Programming Guide":
    https://developer.apple.com/library...on.html#//apple_ref/doc/uid/10000172i-SW1-SW1

    No, because it wasn't launched by 'launchd'.
     
  14. Pimpampoum, Jan 17, 2015
    Last edited: Jan 17, 2015

    Pimpampoum thread starter macrumors newbie

    Joined:
    Jan 10, 2015
    #14
    Thank you chown33.
    When I execute the AppleScript "do shell script "/usr/local/bin/mediatomb --daemon"" MediaTomb runs such as a daemon without to open a Terminal window.
    I can to see it with the command line "top" in the terminal after the launching.

    So, how to do to stop the daemon by leaving the AppleScript ?
    It is what I want.
    It is necessary to add a stance in the script, but which.
     
  15. Pimpampoum thread starter macrumors newbie

    Joined:
    Jan 10, 2015

Share This Page