Awkward shell script in Applescript

Hi,
I'm trying to get the following line of code to run in an Applescript:
ifconfig | grep "inet " | grep 192.168 | cut -d\ -f2
It works perfectly in terminal, but I can't for the life of me to work in an Applescript (I am aware of the double spacing requirements).
Any advice would be most welcomed.

Is this what you want?
do shell script "ifconfig | awk '/inet/ { if (match($2, /192\.168\./)) print $2; }'"

Similar Messages

  • How to retrive ip address of connected device in shell script or applescript

    Hi all,
    From Mac PC, how to get ip address of connected device in shell script or applescript.
    there is any way to launch an app on ipad in shell script or applescript.
    thank you in advance for your help
    Mickael

    Hi all,
    From Mac PC, how to get ip address of connected device in shell script or applescript.
    there is any way to launch an app on ipad in shell script or applescript.
    thank you in advance for your help
    Mickael

  • How to do multiple shell scripts in AppleScript

    I got some solutions from previous posts on how to run sudo in Applescript, but there is still some minor syntax issues when I try to get to the destination directory. Usually in shell script I just type
    cd directory-destination
    in several lines to batch process those commands, but when I work with applescript, if I do do shell script for every "cd" command, instead of getting an overall result, I would get intermediate results individually.
    I read doc and learned that there might be a way to put commands together by using the & sign?
    Message was edited by: ttback

    An individual do shell script command runs in its own shell, so to perform multiple commands within that shell you need to combine them into a single statement. You can use the ampersand '&' operator to concatenate text strings, and the semicolon ';' to separate the commands, for example:set theFolder to "/Applications"
    do shell script "cd " & theFolder & "; ls -l ."See the technical note do shell script in AppleScript.

  • Shell script in Applescript

    I have a program that I run via Terminal, but I don't want a terminal window open while I run it (Just personal preference). I have an applescript set up to run the Terminal command, but the thing is that the command is "on" while the program is running, Is there some way that I can have the command go and then end the script wihtout closing the program? Or am I out of luck and have to have the Terminal window open?
    The program I am using is located here http://sites.google.com/site/sc2gears/
    Thanks for the help!

    The program is distributed (according to the docs) as a .command file, so you can get the efect you want like so:
    do shell script "/Users/yourname/further/path/Sc2gears-os-x.command &> /dev/null &"
    the &> /dev/null tells applescript that you don't care about any output, so it moves on to the next command, and the closing & sets the process up as a standalone.  Note:
    If the process doesn't close itself automatically you'll need a separate way of doing that
    if the process produces output you want to keep, don't use /dev/null - use an appropriate file path
    (obviously) '/Users/yourname/further/path' needs to be replaced with the correct path to the command, and needs to be single-quoted/escaped if it contains spaces or other unix-confounding characters

  • Shell Script or Applescript to run disk permissions repair

    Tried doing this Applescript in Automator:
    do shell script "sudo diskutil repairPermissions /" ¬
      password "yourAdminPassword" with administrator privileges
    This works, but the process appears to run without shutting down when it's done.
    Can anybody suggest an addition or modification to terminate when the permissions repair is completed?

    do shell script ¬
              "sudo diskutil repairPermissions / ; exit" password "yourpassword" with administrator privileges

  • Find & replace part of a string in Numbers using do shell script in AppleScript

    Hello,
    I would like to set a search-pattern with a wildcard in Applescript to find - for example - the pattern 'Table 1::$*$4' for use in a 'Search & Replace script'
    The dollar signs '$' seem to be a bit of problem (refers to fixed values in Numbers & to variables in Shell ...)
    Could anyone hand me a solution to this problem?
    The end-goal - for now - would be to change the reference to a row-number in a lot of cells (number '4' in the pattern above should finally be replaced by 5, 6, 7, ...)
    Thx.

    Hi,
    Here's how to do that:
    try
        tell application "Numbers" to tell front document to tell active sheet
            tell (first table whose selection range's class is range)
                set sr to selection range
                set f to text returned of (display dialog "Find this in selected cells in Numbers " default answer "" with title "Find-Replace Step 1" buttons {"Cancel", "Next"})
                if f = "" then return
                set r to text returned of (display dialog "Replace '" & f & "' with " default answer f with title "Find-Replace Step 2")
                set {f, r} to my escapeForSED(f, r) -- escape some chars, create back reference for sed
                set tc to count cells of sr
                tell sr to repeat with i from 1 to tc
                    tell (cell i) to try
                        set oVal to formula
                        if oVal is not missing value then set value to (my find_replace(oVal, f, r))
                    end try
                end repeat
            end tell
        end tell
    on error number n
        if n = -128 then return
        display dialog "Did you select cells?" buttons {"cancel"} with title "Oops!"
    end try
    on find_replace(t, f, r)
        do shell script "/usr/bin/sed 's~" & f & "~" & r & "~g' <<< " & (quoted form of t)
    end find_replace
    on escapeForSED(f, r)
        set tid to text item delimiters
        set text item delimiters to "*" -- the wildcard 
        set tc1 to count (text items of f)
        set tc2 to count (text items of r)
        set text item delimiters to tid
        if (tc1 - tc2) < 0 then
            display alert "The number of wildcard in the replacement string must be equal or less than the number of wildcard in the search string."
            error -128
        end if
        -- escape search string, and create back reference for each wildcard (the wildcard is a dot in sed) --> \\(.\\)
        set f to do shell script "/usr/bin/sed -e 's/[]~$.^|[]/\\\\&/g;s/\\*/\\\\(.\\\\)/g' <<<" & quoted form of f
        -- escape the replacement string, Perl replace wildcard by two backslash and an incremented integer, to get  the back reference --> \\1 \\2
        return {f, (do shell script "/usr/bin/sed -e 's/[]~$.^|[]/\\\\&/g' | /usr/bin/perl -pe '$n=1;s/\\*/\"\\\\\" . $n++/ge'<<<" & (quoted form of r))}
    end escapeForSED
    For what you want to do, you must have the wildcard in the same position in both string. --> find "Table 1::$*$3", replace "Table 1::$*$4"
    Important, you can use no wildcard in both (the search string and the replacement string) or you can use any wildcard in the search string with no wildcard in the replacement string).
    But, the number of wildcard in the replacement string must be equal or less than the number of wildcard in the search string.

  • I can't get a simple "do shell script" in AppleScript to work!

    Hi All,
    I can get simple commands like "do shell script "ls"" or "do shell script "ps"" to work in AppleScript, but I cannot get something like "do shell script "python -V"" to work. (yes, my shell script says: do shell script "python -V" (no double quotes!))
    This is driving me nuts!
    Can anyone help?
    - Jon

    python -V appears to direct its output to stderr. Try
    do shell script "python -V 2>&1"

  • How do I run a shell script via AppleScript?

    Seems obvious, "do shell script" but that doesn't work for me.
    I have an Automator app which runs a shell script, I'd like to take that into an AppleScript. My AS doesn't run the shell script. Perhaps "do shell script' is expecting the script to be located elsewhere? The rest of my AS runs fine, but the shell script doesn't.
    repeat with i from 1 to count of items in exportFolder
      do shell script
              "exiftool -overwrite_original -Photoshop:CopyrightFlag='True'"
      done
    end repeat
    As I say this works within an Automator app, what changes do I need to make it work in AppleScript?
    Thanks!

    Camelot wrote:
    repeat with i from 1 to count of items in exportFolder
    What is 'exportFolder'? Where is it defined?
    Thanks, 'exportFolder' is defined earlier in the script. The purpose of the script - which is run from within Aperture - is to rename the selected images, export them (to the 'exportFolder'), reset the version name and, using ExifTool, add metadata which Aperture does not write to exported JPEGs.
    I have a version of this which works in Automator but I couldn't see any clues there on setting the path to ExifTool.
    Here's the full (modified) script. Currently it returns an error
    Result:
    error "No file specified" number 1
    so I now need to understand if I can use 'exportFolder' (defined at the start) to tell Finder which folder to use.
    -- Creating filenames by making Version Name  from the IPTC Headline and Filename (with hypens where spaces exist). The Aperture Version Name is used to create the file's name on export. Once exported the Aperture Version Name is reset to its original.
    tell application "System Events"
              set exportFolder to (choose folder with prompt "Choose an export folder")
    end tell
    tell application "Aperture"
              set theSel to (get selection)
              if theSel is {} then
                        error "Please select an image or two!."
              else
                        repeat with theImg in theSel
      -- Creating a new Aperture Version Name which will become the exported file's filename.
                                  tell theImg
                                            set headline to (get value of IPTC tag "Headline" of theImg)
                                            set AppleScript's text item delimiters to " "
                                            set theTextItems to text items of headline
                                            set AppleScript's text item delimiters to "-"
                                            set headline to theTextItems as string
                                            set AppleScript's text item delimiters to {""}
                                            set objectName to (get value of IPTC tag "ObjectName" of theImg)
                                            set newVersion to (headline & "-" & objectName) as string
                                            set name of theImg to newVersion
                                  end tell
                        end repeat
      -- Exporting the files as JPEGs to chosen folder/Project Name using the Version Name as a filename
                        export theSel using export setting "JPEG - Original Size" to exportFolder
      -- Resetting the Aperture Version Name back to filename using IPTC Title (which should be the file's filename without suffex).
                        repeat with theImg in theSel
                                  tell theImg
                                            set title to (get value of IPTC tag "ObjectName" of theImg)
                                            set name of theImg to title
                                  end tell
                        end repeat
              end if
    end tell
    --Using ExifTool to set Photoshop Copyright Status etc
    tell application "Finder" to set theFiles to files of exportFolder as alias list
    repeat with eachFile in theFiles
              do shell script "/usr/bin/exiftool -overwrite_original -Photoshop:CopyrightFlag='True' -Photoshop:URL='http://davidgordon.co.uk/'" & quoted form of POSIX path of eachFile
    end repeat
    display dialog "Done that!" with icon note buttons "OK" default button 1 giving up after 10

  • Syntax for inserting a number in a shell script in applescript

    who can help me correct this code?
    the user will enter a number in a dialog box, and this variable will replace the 7 in the shell script.
    i cant seem to keep the syntax right, i keep erroring out.
    if days_back is "" then
    set dialog_1 to display dialog "please enter a number " default answer ""
    set the days_back to the text returned of dialog_1
    end if
    set the_result to (do shell script "mdfind -onlyin '/Volumes/Volume1' 'kMDItemFSContentChangeDate >=$time.today(-7)'")
    display dialog the_result

    Try using:
    if days_back is "" then
    set dialog_1 to display dialog "please enter a number " default answer ""
    set the days_back to the text returned of dialog_1
    end if
    do shell script ("mdfind -onlyin '/Volumes/Volume1' 'kMDItemFSContentChangeDate >=$time.today(-" & days_back & ")'")
    display dialog the result
    (54186)

  • Can I embed a full shell script inside an applescript?

    I have a friend who is running Tiger on a PPC Mac and wants to download a large number of files from the web. Unfortunately, this friend is barely able to do basic web browsing with Safari or Firefox.
    I thought of just sending him a shell script with a lot of curl commands, but I don't suppose making it executable on my Mac would make it executable on his. I would like to be able to send him an Applescript that he could just run by clicking on it, but it would be awkward to make each curl command a separete shell script within the applescript..
    Is there a way of directly including in an applescript a multi-line shell script as a single entity that invokes only one shell? I know I can do it by putting the shell script in a separate file and have the applescript give it the necessary permissions, but then I'd have to explain to my friend where to put the shell script!

    While it is possible to do this in the Applescript if the shell script gets at all complicated escaping characters and debugging will be much harder then it needs to be.
    For example taking twtwtw's example and just adding one Applescript variable gives:
    set dir to POSIX path of (choose folder)
    set ss to "cd " & dir & "
    echo 'This is a file list for the \"" & dir & "\" folder'
    echo
    ls -l"
    set dlf to do shell script ss
    display alert dlf giving up after 10
    Twtwtw's suggestion of creating an Applescript application bundle is, I believe, the best way to go. You can keep the shell script and Applescript separate making maintenance and debugging much simpler and your friend just gets one 'file' to install and run.
    regards

  • Applescript: display dialog while doing shell script

    Hello there,
    I'm making an applescript app for my company, and had  a question.
    The functionallity of the app is working great, but there is a certain step which can take up to several minutes.
    This can give the user the feeling that nothing is happening and things are stuck.
    Is there a possibility to display a dialog as long as the action (shell script) is running?
    Something along the lines of "Now performing action X. please wait...)
    Thanks for your thoughts!
    Grtz

    With regular AppleScript you can start the shell script in the background (see do shell script in AppleScript) and then put up a dialog, although you would have to periodically check to see if the shell script is finished.  In Lion, the AppleScript Editor has a Cocoa-AppleScript template that you can use (kind of a wrapper application that lets you use various Cocoa methods without having to use Xcode), in which case you could put up an indeterminite progress indicator and then do the shell script.
    AppleScript Studio is deprecated as of Snow Leopard, but there are some AppleScriptObjC in Xcode tutorials at MacScripter.  An additional source of information and templates is macosxautomation, but they seem to be having some server problems at this time.  I also have a Progress Window template application example, it can be downloaded here.

  • Wait for a "do shell script" command to finish

    i have an applescript that has to call a few external applications. i currently do this by setting the exact syntax of the external command in a string variable, then issue a "do shell script cmd" (seen many examples across the net using this standard)
    my applescript continues beyond these commands. how do i set the applescript to either wait for the above external command to finish, or not?
    regards
    jingo_man

    Usually AppleScript will wait for a shell script to complete before it continues, but adding an ampersand (&) at the end of the shell script command will make it run in the background, which allows the AppleScript to continue. See Technical Note 2065: do shell script in AppleScript for more information.

  • Run Shell Script

    Hi,
    I'm new to Automator so I don't really know what to do:
    What I want to do is run this line and create an "application" for my dock
    virtuoso-t -f -c /usr/local/var/lib/virtuoso/ontowiki/virtuoso.ini &
    so I choose "Run Shell Script" Workflow in the Automator Menu. Set the Shell to /bin/zsh (that's what im using) pass input: to stdin.
    The workflow runs with no errors, but the virtuoso server does not start
    any ideas?
    Thanks daniel

    The shell used doesn't have any of your environment variables (it is just a generic shell with no custom settings), so if virtuoso isn't in the standard path it won't be found. It is usually a good idea to use full paths anyway, especially when using Automator's Run Shell Script or AppleScript's do shell script. I am also going to guess that the missing space between the -t option is a typo.

  • Do shell script problem in Applescript

    Hi,
    I am an Applescript novice and have been trying to write a code to go to a particular folder, look for all files in the folder tree with extension .m2v and run an executable file to decode them. My problem is that when I run my code (containing do shell script), it searches through all files and folders on Mac HD and starts decoding .m2v files elsewhere that I don't want.
    Eventually it runs out of space (.m2v file decoding takes a lot of space), because it is dumping all decoded .yuv files onto the HD.
    When I run the command on Terminal, it executes the decoding perfectly and stores the decoded files in the same folder.
    Please help me about what's going on.
    My code is something like:
    tell application "Finder"
    set DestinationFolder to "xxxxxx:xxxx:xxxx"
    set NumFolders to (get count of folders under Destination folder)
    repeat for SomeVar from 1 to NumFolders
    set FolderinQuestion to folder SomeVar of DestinationFolder
    -- Tried tell application "Terminal" here, but did not know --how to export the FolderinQuestion variable from Finder to --Terminal
    do shell script " \" cd \" & (POSIX path of (result as text));
    for file in `find $pwd \"*.mov\"`
    do
    /usr/local/bin/decode file
    done"
    end repeat
    end tell
    I would greatly appreciate some guidance.

    The root of the problem is that you're trying to quote the cd command for some reason:
    <pre class=command>do shell script " \" cd \" & (POSIX path of (result as text));
    ...</pre>
    In addition to that you're including the & (POSIX path of (result as text)) as part of the shell command whereas this should be OUTSIDE of the quotes in order to get evaluated
    If you work that through you'll end up with a shell command that looks like:
    <pre class=command>" cd " & (POSIX path of (result as text))</pre>
    If you try to run that in a terminal you'll get a cd : command not found error and that's why the rest of it appears to fail.
    The solution to that one is simple - just don't bother quoting the cd and put the POSIX path stuff outside of the quotes to get it evaluated at runtime:
    <pre class=command>do shell script "cd " & quoted form of POSIX path of (FolderInQuestion as text)) & ";
    # rest of shell commands here"</pre>
    Now, as for the rest of the script there are a few things I would change.
    First, unless you need to know the index, don't do:
    >repeat for SomeVar from 1 to NumFolders
    set FolderinQuestion to folder SomeVar of DestinationFolder
    the issue is that the number of folders to process may change during the script's execution (other processes may create or remove folders). This will, at best, cause some folders to be skipped and, at worst, cause the script to fail.
    If you're iterating through a list, the best option is to just:
    <pre class=command>repeat with FolderInQuestion in (folders of DestinationFolder)
    ...</pre>
    This automatically sets the iterator (in this case, FolderInQuestion, to the first item in the list and increments it for each iteration through the loop.
    Secondly, in your shell script itself, scrub the entire do/done loop. You're already using find, so have that do the hard work using the -exec switch:
    <pre class=command>find path -name "*.mov" -exec /usr/local/bin/decode {} \;</pre>
    In find's case, {} is substituted with the current file's path.
    Putting this together you'd get:
    <pre class=command>tell application "Finder"
    set DestinationFolder to "xxxxxx:xxxx:xxxx"
    repeat with folderInQuestion in (get folders of folder DestinationFolder)
    do shell script "cd " & quoted form of POSIX path of folderInQuestion & "; find . -name \"*.mov\" -exec /usr/bin/decode {} \\;"
    end repeat
    end tell</pre>
    Note that I've used 'quoted form of POSIX path' - this takes care of any shell-unsafe characters like spaces in the path name. I've also used \\; for the -exec switch - this is so that AppleScript passes the \ to the shell command rather than using it for its own escaping.
    But you're not done yet!
    There's still one fatal flaw in this process - and that is the fact that find by default, is recursive - it will walk through every directory that it finds.
    This means that if you start at the top folder and iterate through, find will find all .mov files and decode them. Your script then cd's to the first subdirectory and repeats the process - decoding all the .mov files in that directory and all its subdirectories even though they've ALREADY been decoded.
    The upshot is that you only need to run one loop starting at the top level. You don't need to iterate through all the subdirectories since find will do that for you.
    In addition to that, there might not be a need to use cd at all since the first argument to find is the directory to start searching in. Unless there's some reason that you need to start decode from the top level directory (e.g. is that where it saves the files?), you can drop the whole repeat loop altogether and just run with:
    <pre class=command>set startFolder to (choose folder)
    do shell script "find " & quoted form of posix path of startFolder & " -name \"*.mov\" -exec /usr/bin/decode {} \\;"</pre>
    That's the entire script - a radical compression of your original.

  • Problem with backtick replacing apostroph in applescript/shell script

    I've got a script that appears to be using a backtick instead of an apostrophe which is causing an error in my shell script. For the life of me I can't seem to find where the error is being generated?
    The script is attached below. I'm using Exiftool, an app that writes metadata to image files. The shell script
    set cmd to "exiftool -CopyrightNotice=" & exifCopyright & " " & thisFilePath & ""
    set theResult to do shell script cmd
    works fine but the following shell script
    set cmd to "exiftool" & space & authorStr & " " & thisFilePath & ""
    set theResult to do shell script cmd
    returns the error "sh: -c: line 0: unexpected EOF while looking for matching `''
    sh: -c: line 1: syntax error: unexpected end of file" number 2. The code in the event log in applescript editor looks exactly the same to me but one fails in the shell script.
    It has been suggested by the developer of Exiftool, Phil Harvey, that there is a backtick in the second shell script. I read somewhere in the applescript docs that this is due to a change in OS 10.6? Any suggestions on how to fix this?
    Thanks.
    Pedro

    Yea, the authorStr value has a space like "Joe Smith"
    Then you need to use quoted form of this string, too:
    set cmd to "exiftool " & quoted form of authorStr & space & thisFilePath
    although the format looks wrong to me - shouldn't there be some kind of switch, such as "-author=' before it?
    You have to consider how you'd enter this at the command line to work out how best to translate it to AppleScript. For example, if the command line version were:
    exiftool -author='John Doe' /path/to/some.jpg
    you can see the quotes are around the name, not the entire -author switch. In this case you should be looking at something like:
    set authorStr to "John Doe"
    set theFilePath to "/path/to/some.jpg"
    set theCmd to "exiftool -author=" & quoted form of authorStr & space & quoted form of theFilePath
    Now you could, of course, use quoted form when you create the variables (e.g. set authorStr to quoted form of "John Doe"), but that may screw you up later on if/when you try to use authorStr in some other way, so I find it best to use quoted form only where it's needed.

Maybe you are looking for

  • Unable to install 2nd SCOM 2012 Management Server - Services Fails and fails trying to get data reader writer accounts?

    2<sup>nd </sup>Management Server Setup fails at "Data Warehouse Configuration". error log: [12:33:20]:           Always:                :Current Action: StartServices [12:33:20]:           Always:                :Starting OM Services. [12:33:20]:    

  • Mail app no longer groups threads after turning Push to off

    Hey guys, My Mail application was grouping related emails/threads together. Yesterday I decided to switch to Fetch as opposed to Push to conserve battery life in general (which it DEFINITELY makes a huge difference in battery life for fetch every 15

  • E-Recruitment Maintain Substitute

    Hi Expert, We are implementing E-Recruitment, and i having trouble to find out how the Maintain Substitute works in MSS. When manager A maintain the substitute to manager B for substitute the creation of requisition, i could not find where a dropdown

  • APP-V Packaging Web Client

    Hi - I am working on a project where I need to package The Microsoft Azure Remote App available here: https://www.remoteapp.windowsazure.com/ClientDownload/Windows.aspx The systems this app will be installed on, the users have no admin rights and app

  • Production version consistency check

    Hi, I am facing an issue in which i have created a BOM and a master recipe group. The production version is created and all the things are maintained properly so that consistency check shows green light i.e. everything is fine. Then i delete the same