How to Turn Bash Scripts Into Clickable Apps Using AppleScript

bash-script-applescript

Say you’re a bash scripting wizard who loves automating tasks for your less tech-savvy friends and family. Even though you’ve given them a script that completely fixes their problem, they might still be anxious about using the Terminal on their own. In this case, you might as well just do it all for them manually.

Fortunately, AppleScript is a user-friendly language that can easily take your scripts and turn them into something both shareable and GUI friendly.

Getting Started With AppleScript

AppleScript is an age-old programming language that dates all the way back to Mac OS 7. It was designed as an easy and readable language by the standards of users in the 1980s. Its primary purpose is to interact with Finder in order to automate workflows.

You can use it to take a complex workflow, such as a bash script or the modification of a preference file, and simplify it to nothing more than the click of a button. You can also take input in the form of files that you drag and drop on top of it.

Note: Start with our introductory guide to AppleScript syntax and workflow.

The Script Editor tool in your Utilities folder is an AppleScript text editor and IDE that will help you write a functional AppleScript. Its Help menu bar item is an invaluable resource. Use it to load the AppleScript Language Guide.

You can also go to Window > Library to get a list of every possible AppleScript command.

Make Your Bash Scripts Clickable With AppleScript

Bash can help you interact with your Mac in a surgical, hyper-specific way. It’s possible to script almost anything you want to do to your machine.

Normally, if you want to run a bash script, you have to ensure that it has executable permissions. Then you must open Terminal, navigate to the script path, and hit Return to run it. With AppleScript, however, you can run your bash scripts with a quick double-click.

The “do shell script” Command

Adding your bash script to AppleScript is a simple process. First, go to Applications > Utilities > Script Editor to open a new AppleScript. Then, choose a location for your finished script and hit New Document. You’ll see a blank editor window.

Prepare a bash script using your favorite macOS text editor, or do it right in Script Editor. When you’re ready, add it using the do shell script command. You can send a command using:

do shell script "Command"

Add a semicolon (;) to send multiple commands, as in:

do shell script "Command1; Command 2"

To refer to an existing bash script that is somewhere else, simply use:

do shell script "/path/to/your/script.sh"

If you want to run a command that requires administrator privileges, you can set them in the AppleScript first, like so:

do shell script "command" user name "USER" password "PASSWORD" with administrator privileges

Hit the Play button to run through your AppleScript and check it for syntax errors. Finally, save your AppleScript; under File Format, choose Application. This will allow you to double-click to run it.

Reverse It Up in Bash

As a side note, what if you wanted to do the reverse and add AppleScript commands to your bash script? Well, you can do that too!

In your bash scripts, you can add an osascript command, followed by your AppleScript, to add fun elements like user input or alert banners.

Using AppleScript Droplets

Applescript droplet icon

Say you have a load of pictures that you need to resize to get ready for your blog, or you want to sort all your files by type. You can write scripts to accomplish these actions, but a traditional bash script requires you to write the full path of every file you want to manipulate each time you want to do it. That can become tedious when doing a big batch of files.

Fortunately, you can set up an AppleScript that only requires you to drag your target file on top of it to run it as input. These special AppleScripts are referred to as Droplets. Here’s how to create one:

  1. Open a new AppleScript with Script Editor.
  2. Start the AppleScript with the handler on open dropped_file. You can call the dropped_file anything you want. AppleScript will use it as a variable that is assigned to the file you dropped.
  3. Finish the rest of your script, using do shell script or regular AppleScript syntax. Make sure that you close your script with end open to close out the handler.
  4. Save your AppleScript as an application, as explained above.

Now you can drag and drop files directly onto your scripts.

An Example AppleScript Script

Applescript editor window

The example below shows both these handy features in action.

Change the Permissions of Any Drag-and-Dropped File

Say you wanted to give a file, script, or document to a friend and wanted to make sure they couldn’t mess around with it. Using this script, you can easily take away the write permissions of any file to give them a read-only version of it.

Keep in mind, that when you drop a file on top of a Droplet, its path name is read by AppleScript in HFS format (Hierarchical File System). This uses colons, and does not escape spaces, so a file on your desktop reads as:

Macintosh HD:Users:jdoe:Desktop:myfile

Bash uses a different path standard called POSIX, so the same file in bash is read as Macintosh\ HD/Users/jdoe/Desktop/myfile. The script below converts the file name to the appropriate path standard before changing permissions on it:

on open dropped_file
  set bash_path_file to the POSIX path of dropped_file
  do shell script "chmod -R -w " & quoted form of bash_path_file
end open

Use Automator for a More Mac Automation

Now that you’ve got the automation itch, scratch it by diving into Automator on your Mac. Automator and AppleScript have a lot of similarities. If you’re just getting started in the world of scripting and automation, you may find Automator an easier gateway. Instead of a readable programming language, Automator uses intuitive drag-and-drop bubbles and an easy-to-learn interface to simplify your mundane tasks.

Read the full article: How to Turn Bash Scripts Into Clickable Apps Using AppleScript



from MakeUseOf http://bit.ly/2OTMgpL
via IFTTT
Share on Google Plus

About Oyetoke Toby

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment