AppleScript syntax do shell scripts

Hi guys,
i am having endless issues with the syntax of the following applescript. Please help me and i'll make you my new god, cheers. The
applescript syntax is:
tell application "Finder"
  display dialog ¬
                    "Select Output Source" buttons {"Computer", "TV"} default button "TV"
          set InternalSpeakers to (do shell script "cd ~/Downloads/SwitchAudioSource-v1; ./SwitchAudioSource -s 'Built-in Output'")
  --InternalSpeakers is now a variable which changes the sound output to Computer
          set HDMI to (do shell script "cd ~/Downloads/SwitchAudioSource-v1; ./SwitchAudioSource -s 'Soundflower (16ch)'")
  --InternalSpeakers is now a variable which changes the sound output to TV
          if button returned of result = "Computer" then InternalSpeakers
          if button returned of result = "Computer" then HDMI
end tell
at the moment i am getting the following error:
I have tried arranging and re-arranging the applescript to no avail, please help me

You're over complicating the situation. This
set InternalSpeakers to (do shell script "cd ~/Downloads/SwitchAudioSource-v1; ./SwitchAudioSource -s 'Built-in Output'")
  --InternalSpeakers is now a variable which changes the sound output to Computer
sets InternalSpeakers to what the do shell script returns. It does not set InternalSpeakers to hold the do shell script command to run when you access the variable. There are ways to do this but its not needed here. Just do thsi as a simple if then else
display dialog ¬
                    "Select Output Source" buttons {"Computer", "TV"} default button "TV"
if button returned of result = "Computer" then
     do shell script "cd ~/Downloads/SwitchAudioSource-v1; ./SwitchAudioSource -s 'Built-in Output'"
else    if button returned of result = "TV" then
    do shell script "cd ~/Downloads/SwitchAudioSource-v1; ./SwitchAudioSource -s 'Soundflower (16ch)'"
end if
Something like this will work.  Also there is no reason for the code you show to do it in a tell block. The Finder isn;t needed for this.

Similar Messages

  • Automator/Applescript not running shell script properly

    I can open Terminal, and this command works:
    lame -h --abr 256 /Users/myhome/Desktop/Some\ Wav\ File.wav /Users/myhome/Desktop/TheMP3.mp3
    But when I try to run the same command in applescript from Automator, it fails.
    set sourceFile to quoted form of POSIX path of "/Users/myhome/Desktop/Some Wav File.wav"
    set mp3File to quoted form of POSIX path of "/Users/myhome/Desktop/TheMP3.mp3"
    set command to "lame -h --abr 256 " & sourceFile & " " & mp3File
    display dialog command
    do shell script command
    The display dialog outputs:
    lame -h --abr 256 '/Users/myhome/Desktop/Some Wav File.wav' '/Users/myhome/Desktop/TheMP3.mp3'
    Which, as little as I know of applescript, is how applescript handles spaces in filesnames. Can anyone please help?

    You need *do shell script* attribute described in the 10.4 Changes section of http://developer.apple.com/mac/library/releasenotes/AppleScript/RN-AppleScript/R N-104/RN-10_4.html#//appleref/doc/uid/TP40000982-CH104-SW1 Additional info can be gained by posting to the AppleScript and Unix forums under OS X Technologies.

  • Port AppleScript to bash shell script

    Hi all,
    I made this script with the help of Neil from these forums. The purpose of the scrip is so that my friend can set up a share point on his NAS which is simply full of links so that if people access the NAS (its full of music and movies) if the delete a link, the file stays where it is.
    The issue he has now, is that when he make the links in MacOS, a Windows system will no follow them. Is there a way around this or should the script be ported to a bash script so that the links can be created internally on his BSD NAS.
    Thanks for any help and suggestions.
    set these_items to (choose file with prompt "Choose files to link:" with multiple selections allowed)
    set link_folder to (choose folder with prompt "Choose folder to place links in:")
    repeat with this_item in these_items
    tell application "Finder" to reveal this_item
    set this_path to the quoted form of the POSIX path of this_item
    set shell_folder to the quoted form of the POSIX path of link_folder
    do shell script "ln -s" & space & this_path & space & shell_folder
    end repeat
    end

    Your issue is one of symlink support, so it won't matter how you create the symlinks.
    First off, which version of Windows are you running?
    Only later versions of Windows (Win7 and, I think Vista) support symlinks, so if you're using anything earlier it won't work.
    Secondly, if you're pointing to a network volume then the target paths have to match, and that's likely to be a problem in cross-platform environments. That's because the symlink file really just contains the path to the target and your paths may be different on each OS.
    For example, On your Mac the sharepoint might be mounted at /Volumes/server and your file might be at /path/file on that share.
    So your symlink file will contain '/Volumes/server/path/file'
    No Windows system is going to be able to follow that path since disks aren't mounted under /Volumes. You would either have to know that share's current mount point or use a Windows path of the form \\server\share\path\file but that latter form won't work on non-Windows systems.
    So the short answer is that you probably have to create two sets of links, one for the Windows users and one for the Macs. There's no simple, universal, cross-platform symlink standard that will do what you want (at least that I'm aware of).

  • How to embed applescript into a shell script for notification center

    I have managed to cobble this together:
    You can run it in your Terminal, it is harmless, and will output results that will show you a notification from notification center:
    I have items on my pasteboard as the results of a script, I want to pbpaste them into the below command, but can't get the syntax right.  I am not objectionable to string concatenation and building or whatever it takes.
    osascript -e 'display notification "The url(s) are on your clipboard from my.app" with title "my.app has finished" subtitle "Some Sub title"'
    Ideally, I want to put `pbpaste` in the "Some title" section, but I get the literal string. I try creating a variable, and get that literal string.
    I tried building the entire string up as one variable, which in the Terminal echo'd out and works, but in Notification Center, I get the actually literal variable.  It's mainly a bash script, so the context witch to AppleScript i being a pain in the rear.
    Suggestions? Any master out there that this makes any sense to?

    As it stands, and the way you have your notification structured, this is the output from the notification center. I did an echo "From the clipboard" | pbcopy before I ran the following script. It uses a Bash function, osascript HERE document, and shows how to pass variables to the function, and receive them in AppleScript. If you want substring text that is not the result of pbpaste, then you can either make the string inside the AppleScript, or pass the string in as a variable.
    To get this output, I used the following Bash/Osascript combination:
    #!/bin/bash
    #osascript -e 'display notification "The url(s) are on your clipboard from
    #my.app" with title "my.app has finished" subtitle "Some Sub title"'
    function notification () {
      `osascript <<-AppleScript
            set txt1 to "The URL(s) are on your clipboard from "
            set txt2 to " has finished"
        set appName to "$1" as text
        set subStr to "$2" as text
        set noteStr to txt1 & appName
        set titleStr to appName & txt2
        tell application "System Events"
            display notification noteStr with title titleStr subtitle subStr
            delay 1 -- give notification time to fire
        end tell
        return quit
    AppleScript`
    appName="foo.app"
    #echo $appName
    subTitle="$(pbpaste)"
    #echo $subTitle
    notification "${appName}" "${subTitle}"
    exit 0

  • 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

  • Speed up shell script execution

    Hi All,
    Before I go on, I must stress I am doing this for a disk image that I OWN, it's in no way an attempt to break into someone else's data...
    Last week I created an encrypted disk image (10GB) but have since forgotten the password stupidly!!!
    So, I figured I could knock up an applescript to try and brute force the image.
    I have successfully done this running th following code:
    do shell script "hdiutil attach -passphrase PASSWORDVAR diskimage.dmg
    The PASSWORDVAR changes on every loop to the next consecutive attempt, ie
    aaaaa
    aaaab
    aaaac etc
    When I run the script the command is looping about twice a second which is pretty slow... Is there anyway to speed this up a bit? Any help would be great!
    Thanks,

    You don't show the rest of your script that's building the password to try, so it's possible the problem is there, but the chances are your delay is not in AppleScript, but in hdiutil.
    On its own AppleScript can execute simple loops pretty quickly. However in this case you're calling hdiutil which has to take the parameters, test the password against the disk image, verify the result and then return an error.
    There is some overhead here in AppleScript calling do shell script so you might find better performance if this was a single shell script rather than an AppleScript, but I don't think it'll be a lot different.
    If you have multiple machines you can try to divide and conquer, having each machine start at a different point in the list of possibilities (or one start from the end and work forwards), other than that it's a let-it-run-all-night (week?) kind of thing.

  • 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.

  • 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)

  • 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.

  • Shell script friendly paths in applescript

    I have a python script that resides inside a standalone application bundle. I run this script from the app and call it by getting the path of the application bundle and adding '/Contents/Resources/' to the path. This script sets or gets information from a plist. So, adding a ' --list' to the script name returns the items of the plist.
    This is my applescript and RWplist is the python script.
    set plInfo to do shell script "/Users/[just me]/Desktop/Test Project/Test App.app/Contents/Resources/RWplist --list'
    This fails due the the spaces in the directory and app name. If I change the folder and app name like;
    set plInfo to do shell script "/Users/[just me]/Desktop/Test-Project/Test-App.app/Contents/Resources/RWplist --list'
    This works... I tried wrapping the path in single quotes, I tried to do a replace all spaces to '\\space' in the path... and adding the 'quoted form of POSIX path of'...
    I'm guessing I'm placing the single quotes or using the quoted form... improperly. Can someone please explain how to format a path in this manor. I can't seem to find the proper syntax for this.

    This fails due the the spaces in the directory and app name
    Right, this is a common problem that many people encounter.
    I'm guessing I'm placing the single quotes or using the quoted form... improperly. Can someone please explain how to format a path in this manor.
    quoted form is the preferred/recommended way of doing this. Here's an example:
    set cmdPath to "/Users/just me/Desktop/Test Project/Test App.app/Contents/Resources/RWplist"
    do shell script (quoted form of cmdPath) & " --list"
    If you want to build the script manually then you should single-quote the entire path, e.g.:
    do shell script " '/Users/just me/Desktop/Test Project/Test App.app/Contents/Resources/RWplist' --list"
    Note that there are single quotes around the command path but the --list parameter is outside of the single-quoted path.

  • 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

  • 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

  • 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.

  • 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

Maybe you are looking for

  • Will this tiger woods pga '12 work on lion ??

    heres the link!! Thanks!! http://www.amazon.com/Tiger-Woods-PGA-TOUR-12/dp/B005IRLHBG/ref=sr_1_12?ie=UTF8& qid=1316123742&sr=8-12

  • Sending Sync Request to two Different Receivers.

    Hi, I have a scenario where the synchronous request with single operation has to be sent to two receivers(different systems) with one being async and another one being sync. The response from the second receiver has to be mapped to sender. Though thi

  • Cash Receipt Entries

    Hi Everybody, My issue is, How to do the Entries for Cash receipts without PO reference We have cash receipts, with Taxes as mentioned below: 1. Vat 4% , 12.5 % , or any other percent. 2. Service tax S1, S2 and S3. we would like to do direct entry in

  • Merge columns in an ALV Table

    Hi colleagues, is it possible to merge two or more columns in ALV Table (like ColSpan in the Matrix-Layout) to write a text over more than one column? I haven't found any solution for it until now. Best regards Stefan

  • Arabic text running the wrong way

    I need to place a series of words, in Arabic, in an array. When I copy the words from the original text document to the script window, the order of the letters reverses from right-to-left to left-to-right, so basically all the Arabic is being written