Results 1 to 8 of 8
Discuss Command Line Service using Applescript at the Genuine Mac Support - Hackint0sh.org; Ok, so I don't know if there is a website better suited for the question ...
  1. #1
    Advanced Array

    Join Date
    Sep 2010
    Posts
    30
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default Command Line Service using Applescript

    Ok, so I don't know if there is a website better suited for the question I have, and I don't know for sure if this is the correct forum on this site for this question, but here goes anyhow. I have a new Intel Mac Mini running Snow Leopard. What I want to do is create several services that run command line actions on a user-selected file from Finder(and most of the command line actions I wish to run, I wish to run as administrator/superuser). I am pretty sure the best way to do this would be using Applescript inside Automator. The part I can't figure out is how to take the filename of the selected file(s) and pass into an Applescript. Is there a system-wide variable of some type that holds that filename and the path to it? I think that is all I am missing?



  2. #2
    Super Moderator Array Olethros's Avatar

    Join Date
    Sep 2007
    Location
    Norway
    Posts
    8,360
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    439

    Default

    Automator can support unix shell scrips as well. So you don't need to go through the intermediate of AppleScript
    Please read the stickies & search forum before posting!
    How to report an iTunes restore/update fail in a useful manner
    -

    iPad 3G 64GB (4.3.3, Redsn0w) oldest SHSH 3.2.2
    iPhone 4 32GB (4.2.1, Redsn0w JB-monte) oldest SHSH 4.1
    iPhone 3GS 32GB (4.3.3; Pwnagetool) factory unlocked oldest SHSH 3.1
    iPhone 8GB (3.1.3; Pwnagetool) AT&T Locked - Unlocked with bootneuter

    -
    Did we solve your problem? Got a dollar or two spare ? Donate!

  3. #3
    Advanced Array

    Join Date
    Sep 2010
    Posts
    30
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default

    Doohhh *slaps forehead*....of course, I didn't see that automator action, and I didn't think to look for it...thanks...

  4. #4
    Advanced Array

    Join Date
    Sep 2010
    Posts
    30
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default

    ok, so some of the actions for which I want to create services, I want to run as superuser(or administrative privileges, sudo, etc.) I cannot, however, seem to get the "with administrative privileges" line to work, nor sudo. Is there something I am missing with that?

    Edit: btw, when I say that I can't seem to get the "with administrative privileges" line to work, I am talking about trying to use it in the Automator action, "Run Shell Script".
    Last edited by sailor0703; 12-25-2010 at 06:53 PM.

  5. #5
    Super Moderator Array Olethros's Avatar

    Join Date
    Sep 2007
    Location
    Norway
    Posts
    8,360
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    439

    Default

    Quote Originally Posted by sailor0703 View Post
    ok, so some of the actions for which I want to create services, I want to run as superuser(or administrative privileges, sudo, etc.) I cannot, however, seem to get the "with administrative privileges" line to work, nor sudo. Is there something I am missing with that?

    Edit: btw, when I say that I can't seem to get the "with administrative privileges" line to work, I am talking about trying to use it in the Automator action, "Run Shell Script".
    I haven't tried this, however - one option (only really viable if this automator action is limited to your own machine) is to add specific exceptions for the commands you want to run with sudo/admin rights.

    This can be done by careful editing of /etc/sudoers

    Do your research first and keep a backup of the original file.
    This option is ideal because you are specifically delegating the ability to run only some commands with elevated rights and NOT needing to enter a password. So you limit any potential security breach to the commands that your script needs to run.

    The other option is (not recommended) but much easier to implement.

    This involves going back to applescript

    do shell script "sudo your-admin-rights-only-command" password "YourAdminPassword" with administrator privileges
    replace your-admin-rights-only-command with the actual command you want to run.

    This is a very evil/bad option because it embeds the password into your script for anyone to see.
    Please read the stickies & search forum before posting!
    How to report an iTunes restore/update fail in a useful manner
    -

    iPad 3G 64GB (4.3.3, Redsn0w) oldest SHSH 3.2.2
    iPhone 4 32GB (4.2.1, Redsn0w JB-monte) oldest SHSH 4.1
    iPhone 3GS 32GB (4.3.3; Pwnagetool) factory unlocked oldest SHSH 3.1
    iPhone 8GB (3.1.3; Pwnagetool) AT&T Locked - Unlocked with bootneuter

    -
    Did we solve your problem? Got a dollar or two spare ? Donate!


  6. #6
    Advanced Array

    Join Date
    Sep 2010
    Posts
    30
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default

    ok, so I finally got it figured out, and got one service created. I didn't want to save a password in any script, and really, I was looking to be asked for the password upon running the service, anyhow, just like if I ran the command manually from Terminal. Anyhow, for future reference, this is what I ended up file(I tested it on multiple files and folders in my Trash, which was the idea of this one, to remove individual files from Trash rather than having to empty my entire trash to get rid of a particular file, and it worked each time):


    on run {FileRM, parameters}
    set FileRM to POSIX path of FileRM --Converts input to useable path

    tell application "Finder"
    do shell script "sudo rm -dPR " & quoted form of FileRM & " | sudo -k" with administrator privileges --secure removes selected file or folder after asking for password, then cancels sudo authorization, "quoted form" phrase allows to escape spaces in path
    end tell

    return FileRM
    end run


    I am not sure what exactly the whole on run{} end run thing is for, or if it is even necessary, but Automator put that in automatically when I selected the Automator action, "Run an Applescript". However, I cannot seem to make it work with more than one file at a time. This problem seems to be rooted in either Applescript or Automator, as this script does not allow more than one file at a time either:

    on run {FileRM, parameters}

    display dialog FileRM
    return FileRM
    end run

    any suggestions how that might work, or is that simply a limitation that I cannot work around?

    EDIT: also, this does not seem to work for all file types. Specifically, symbolic links or aliases(whichever it was I just tried), do not work. Is this a limitation of the Applescript or Automator as well? And should it only be that type which cannot be used this way?
    Last edited by sailor0703; 12-26-2010 at 04:41 PM.

  7. #7
    Super Moderator Array Olethros's Avatar

    Join Date
    Sep 2007
    Location
    Norway
    Posts
    8,360
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    439

    Default

    you need to use something like

    Code:
    on run {FileRM, parameters}
    repeat with aFile in FileRM
    
    set aPosixFile to POSIX path of aFile --Converts input to useable path
    
    tell application "Finder"
    do shell script "sudo rm -dPR " & quoted form of aPosixFile & .....
    end tell
    end repeat
    
    return FileRM
    end run
    The key is that with multiple files, the applescript recieves a list, each entry in the list is a file. You loop over the list and perform the same operation on each individual file.
    Please read the stickies & search forum before posting!
    How to report an iTunes restore/update fail in a useful manner
    -

    iPad 3G 64GB (4.3.3, Redsn0w) oldest SHSH 3.2.2
    iPhone 4 32GB (4.2.1, Redsn0w JB-monte) oldest SHSH 4.1
    iPhone 3GS 32GB (4.3.3; Pwnagetool) factory unlocked oldest SHSH 3.1
    iPhone 8GB (3.1.3; Pwnagetool) AT&T Locked - Unlocked with bootneuter

    -
    Did we solve your problem? Got a dollar or two spare ? Donate!

  8. #8
    Advanced Array

    Join Date
    Sep 2010
    Posts
    30
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default

    Ok, that got it, I think! Thanks for all the help! I think I can figure out the rest of the services I want to do in a similar fashion!

 

 

Similar Threads

  1. Command Line Connect to Wi-Fi
    By John6000 in forum Free Toolchain Software (Cydia App's)
    Replies: 4
    Last Post: 02-14-2008, 08:04 PM
  2. Build a one-click app from a command line command?
    By merl in forum Free Toolchain Software (Cydia App's)
    Replies: 2
    Last Post: 11-19-2007, 07:43 PM
  3. ffmpeg command line
    By Korben in forum iPod Touch 1G
    Replies: 2
    Last Post: 11-14-2007, 02:08 PM
  4. Command line stravaganza!
    By nop144666 in forum Free Toolchain Software (Cydia App's)
    Replies: 0
    Last Post: 11-07-2007, 04:02 AM
  5. 1.0.2 - Command line exits on command
    By phattie in forum General
    Replies: 0
    Last Post: 08-24-2007, 12:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Powered by vBulletin®
Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.
Search Engine Friendly URLs by vBSEO
(c) 2006-2012 Hackint0sh.org
All times are GMT +2. The time now is 12:29 PM.
twitter, follow us!