Live Coding Commands with Applescript

Discussion in 'Mac Programming' started by jodagee, Apr 11, 2014.

  1. jodagee macrumors newbie

    Joined:
    Apr 11, 2014
    #1
    Hi Guys,

    I'm relatively new to applescript but I'm quick at exploring and getting up to speed with things. I have been experimenting and was wondering whether it was possible to define commands at runtime?

    For example you could code:
    tell application definedApplication to make new folder ....
    where definedApplication was entered to a dialog at runtime.

    Is it possible to do the opposite and instead of defining the name of the application at runtime, define the command to be used, i.e.:
    tell application "Finder" to liveCommand
    where liveCommand is entered via a dialog?

    Thanks for your time :)
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    You can send variable commands to an application, but you need to specify an application for a command. In some cases application scripting terms may be similar, but in general AppleScript needs to know what the target application is so that it can use its scripting dictionary.

    An alternative would be a completely constructed application tell statement - this is frequently used with osascript or NSAppleScript in Cocoa applications, for example.
     
  3. jodagee thread starter macrumors newbie

    Joined:
    Apr 11, 2014
    #3
    Could you give an example about how to do this in applescript? Everything I put a command even in quotes it prevents it from working. Cocoa is objective c right? I've tried learning that, but I don't even know where to begin! Thanks :)
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Post your code that doesn't work. Use CODE tags.

    Also post the exact text of error messages (copy and paste; don't paraphrase).

    Identify your OS version.
     
  5. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #5
    Cocoa is usually accessed with Objective-C, but it can also be used via AppleScriptObj-C in a Cocoa-AppleScript. Using a shell script to run an AppleScript inside an AppleScript is a bit redundant, but osascript can be useful to run small self-contained scripts, and is often used from other languages such as Ruby or Python. The quoting part is the trickiest, just remember to escape any quotes you are using inside a string, since those are normally used to delimit the string. A simple example would be something like:

    Code:
    set theFile to (choose file) as text
    
    set theScript to "tell application \"Finder\" to get the name of file \"" & theFile & "\""
    set theName to do shell script "osascript -e " & quoted form of theScript
    
     

Share This Page