Quick AppleScript Question

Discussion in 'Mac Programming' started by edesignuk, Jan 28, 2008.

  1. Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England
    #1
    What I'm trying to do:
    Launch Firefox's profile manager.

    How I'm doing it:
    Well, to run profile manager from a terminal window you enter:
    Code:
    /Applications/Firefox.app/Contents/MacOS/firefox -ProfileManager
    I had a google round and the best way to do this is from an AppleScript (saved as "Application Bundle") containing the following:
    Code:
    do shell script "/Applications/Firefox.app/Contents/MacOS/firefox -ProfileManager"
    The problem:
    The AppleScript app launches fine and runs Firefox's profile manager, the problem is the AppleScript app doesn't exit and actually stops responding.

    How do I get the AppleScript to exit cleanly after launching Firefox?

    It must be simple, I'm just utterly clueless when it comes to AppleScript :confused: :eek:

    Thanks!
     
  2. macrumors regular

    Joined:
    Mar 6, 2007
    #2
    When you execute a shell script from Applescript, the script will wait for the output of your shell command before continuing. Since there is no output from the shell command you are executing in your case, the script will wait until you quit the Profile Manager before continuing.

    You could add an ampersand to run the shell command in the background. As soon as the command is executed, the script will continue to run without waiting for any output. If this is the last line of your script, it will exit.
    Code:
    do shell script "/Applications/Firefox.app/Contents/MacOS/firefox -ProfileManager &"
    There is some good info on the do shell command here.

    You could also try to just activate the program from Applescript without using a shell command, although this won't launch it with the -ProfileManager switch.
    Code:
    tell application "Firefox" to activate

    And finally, see if Firefox has a scripting dictionary. If it does, you may be able to access some features directly from your script, without using a shell command.
     
  3. thread starter Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England
    #3
    You're a genius! The first bit you mentioned putting the "&" at the end worked perfectly. Thank you so much :)

    hmmmm, OK, next part of the problem :eek:

    The script to launch the Profile Manager works perfectly as I said. Now to launch in to a specific profile the AppleScript app still hangs.
    Code:
    do shell script "/Applications/Firefox.app/Contents/MacOS/firefox -P edesignuk &"
    Firefox does launch in to the profile I want, but the script sits there in a not responding state.
     
  4. macrumors regular

    Joined:
    Mar 6, 2007
    #4
    :eek: Tell that to my wife, please.
    Now methinks you should also send the stdout and stderr to /dev/null so it is not waiting for any pipes there, either. Try adding "> /dev/null 2>&1" to your line (like this):
    Code:
    do shell script "/Applications/Firefox.app/Contents/MacOS/firefox -P edesignuk > /dev/null 2>&1 &"
    FYI, "2>&1" combines the stdout and stderr into one, since do shell script keeps them separate. And FWIW, I believe the above statement "> /dev/null 2>&1" is the same as just using "&> /dev/null", so for S&G you could try:
    Code:
    do shell script "/Applications/Firefox.app/Contents/MacOS/firefox -P edesignuk &> /dev/null &"
     
  5. thread starter Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England
    #5
    Once again, you're a genius. Works perfectly. Thanks very much again.
     
  6. macrumors 6502a

    Joined:
    Jun 8, 2007
    #6
    old thread but it helped me out today. Im curious though what the syntax actually means

    Can someone explain what it means and is doing at the end of the script? Im learning :)
     
  7. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    > at the end of a command means "redirect the standard out of this command to..." and the filename afterwards is where the output goes
    2> means the same, but for the standard error instead of standard output.
    &1 used after this means to direct standard error to standard output

    & at the end of all of that means to run the command in the background, so it will run (forever if the command does not terminate) out of the way of the program/shell/etc. that launched it. If you did this from a shell, the fg command would bring this program back to the foreground.

    /dev/null is a file that is a black hole. You can send as much data there as you'd like, and it will be eaten up, never to be seen again. This is often used when the output of something is unimportant and needs to be discarded.

    -Lee
     
  8. macrumors 6502a

    Joined:
    Jun 8, 2007
    #8
    Thanks for taking to time to explain. Does memory get used up if you send data to /dev/null ?
     
  9. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    None. Unlike real physics, data can be destroyed by the black hole that is /dev/null.

    -Lee
     
  10. thread starter Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England
    #10
    Snow Leopard has now broken this :(

    Screen shot 2009-08-29 at 09.35.12.png

    Code:
    do shell script "/Applications/Firefox.app/Contents/MacOS/firefox -P edesignuk &> /dev/null & "
    Can anyone help? Pwease! :eek: :)
     
  11. thread starter Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England
    #11
    Trying to figure this out...

    Doesn't look like it's the AppleScript app that's the problem as such, even running the command in Terminal produces the same result (which makes sense I suppose):
    Code:
    /Applications/Firefox.app/Contents/MacOS/firefox -P edesignuk
    Here is the crash report:
    Code:
    Process:         firefox-bin [1241]
    Path:            /Applications/Firefox.app/Contents/MacOS/firefox-bin
    Identifier:      firefox-bin
    Version:         ??? (???)
    Code Type:       X86 (Native)
    Parent Process:  sh [1237]
    
    Date/Time:       2009-08-31 09:08:09.079 +0100
    OS Version:      Mac OS X 10.6 (10A432)
    Report Version:  6
    
    Interval Since Last Report:          34175 sec
    Crashes Since Last Report:           7
    Per-App Crashes Since Last Report:   5
    Anonymous UUID:                      9C9A829A-4598-42D4-8B73-E4AB8B3809B6
    
    Exception Type:  EXC_BREAKPOINT (SIGTRAP)
    Exception Codes: 0x0000000000000002, 0x0000000000000000
    Crashed Thread:  0
    
    Dyld Error Message:
      [B]Library not loaded: /usr/lib/libsqlite3.dylib
      Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
      Reason: Incompatible library version: Security requires version 9.0.0 or later, but libsqlite3.dylib provides version 1.0.0[/B]
    
    Model: MacPro3,1, BootROM MP31.006C.B05, 8 processors, Quad-Core Intel Xeon, 2.8 GHz, 6 GB, SMC 1.25f4
    Graphics: ATI Radeon HD 2600 XT, ATI Radeon HD 2600, PCIe, 256 MB
    Memory Module: global_name
    AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0x88), Broadcom BCM43xx 1.0 (5.10.91.19)
    Bluetooth: Version 2.2.0f18, 2 service, 0 devices, 1 incoming serial ports
    Network Service: AirPort, AirPort, en2
    PCI Card: ATI Radeon HD 2600, Display, Slot-1
    Serial ATA Device: WDC WD6400AAKS-00A7B2, 596.17 GB
    Serial ATA Device: SAMSUNG HD753LJ, 698.64 GB
    Serial ATA Device: SAMSUNG HD753LJ, 698.64 GB
    Serial ATA Device: WDC WD3200AAJS-41VWA0, 298.09 GB
    Parallel ATA Device: PIONEER DVD-RW  DVR-112D
    USB Device: Keyboard Hub, 0x05ac  (Apple Inc.), 0x1006, 0xfd500000
    USB Device: USB/PS2 Wheel Mouse, 0x047d  (Kensington), 0x1012, 0xfd530000
    USB Device: Apple Keyboard, 0x05ac  (Apple Inc.), 0x0221, 0xfd520000
    USB Device: USB Receiver, 0x046d  (Logitech Inc.), 0xc50e, 0x3d200000
    USB Device: Bluetooth USB Host Controller, 0x05ac  (Apple Inc.), 0x8206, 0x5d200000
    FireWire Device: built-in_hub, Up to 800 Mb/sec
    
    The bit I have bolded looks to be the significant bit.

    Is there anything I can do about this? Reeeeeally missing the ability to have multiple Firefox's running with different profiles at once :eek:
     
  12. macrumors 6502

    Joined:
    Sep 15, 2006
    #12
    Wouldn't a much simpler solution be to simply use automator? You can execute most applications - and failing that you can simply call firefox from the shell - which automator can do as well.

    I can elaborate on this later if you want (kind of at work atm :eek: )
     
  13. thread starter Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England
    #13
    ^ It wasn't the AppleScript that was the problem, so I don't really see how Automater would have helped.


    Anyway, on searching I found a bug thread at Mozilla about this.

    A user there suggested invoking firefox-bin directly rather than just firefox, and it works!

    So, doesn't work...
    Code:
    do shell script "/Applications/Firefox.app/Contents/MacOS/[B]firefox[/B] -P edesignuk &> /dev/null & "
    Works...
    Code:
    do shell script "/Applications/Firefox.app/Contents/MacOS/[B]firefox-bin[/B] -P edesignuk &> /dev/null & "
     
  14. macrumors regular

    Joined:
    Mar 6, 2007
    #14
    I'm glad you got it working. ;) This is probably not the last thing we'll find to be broken by Snow Leopard, either.

    Cheers.
     
  15. thread starter Moderator emeritus

    edesignuk

    Joined:
    Mar 25, 2002
    Location:
    London, England
    #15
    Hi! Thanks for coming and taking a look, but yes, I have it fixed now, nothing to do with AppleScript at all in the end :)
     
  16. macrumors newbie

    Joined:
    Aug 20, 2009
    Location:
    San Diego
    #16
    .
     

Share This Page