3

I want to be able to search in Spotlight for a shell script (or something that calls a shell script) and that such shell script opens a Terminal window so the output is shown live (and I can interrupt with ctrl + C).

Right now, I have a backup.sh

#!/bin/bash
caffeinate -s \
  rsync -hva --progress --stats --delete "/path/to/source/" "/path/to/destination"

that I've renamed to backup.command so I can doubleclick in Finder and it works perfectly. But I cannot search for it in Spolight because I want Spotlight to only search for Applications, System Preferences and Contacts, and if I mark Developer then it shows backup.command but also a lot of unwanted files that are just unnecessary.

What can I do?

I tried to appify it but then it runs on the background, and doesn't open a new Terminal window so I can't see the progress of how is it going, and also I can't stop the progress with ctrl + C. Any suggestion?

7
  • maybe this is possible: appify an script, which opens a terminal and execute your script inside this terminal.
    – hirnwunde
    Feb 3 '16 at 13:24
  • You can exclude directories containing unwanted Developer files from beeing shown in Spotlight. You can do it in Spotlight -> Privacy (You can drag folders from finder there also). Feb 3 '16 at 13:37
  • @hirnwunde That would be great, but I don't know how.
    – Manuel
    Feb 3 '16 at 14:07
  • @MateuszSzlosek I would like to exclude everything.
    – Manuel
    Feb 3 '16 at 14:13
  • Is there some good reason why you can't just call it backup, place it in the $PATH and just call it from a Terminal by typing backup and press Enter? Also why not just use Time Machine, it seems it would be much easier then all you've been going through to get this to work. Feb 3 '16 at 14:43
6

Appifying with Platypus:

Download Platypus and create an app from the shell script.

Example:

enter image description here

The settings above create an app with a progress bar (including details) and it can be quitted - the rsync process has to be canceled (killed) manually though.

enter image description here

The app itself can be found with Spotlight.


Simple AppleScript solution:

In Mavericks (and probably later) you have to allow the AppleScript app to control the computer in "System Preferences -> Security & Privacy -> Privacy -> Accessibility"

Open ScriptEditor, enter the code snippet below and export it as an application with a unique name:

tell application "Terminal"
    delay 2
    set newWindow to do script "caffeinate -s rsync -hva --progress --stats --delete /path/to/folder /path/to/folder2"
    activate
end tell

If you need root permissions prepend sudo -> "sudo caffeinate -s ...."

You can refine the AppleScript app by checking if Terminal already has open windows and create a new one to run the shell script:

set myScript to "caffeinate -s rsync -hva --progress --stats --delete /path/to/folder /path/to/folder2"
tell application "System Events"
    if exists (window 1 of process "Terminal") then
        tell application "Terminal"
            set newWindow to do script myScript
            activate
        end tell
    else
        tell application "Terminal"
            delay 2
            set Window1 to do script myScript
            activate
        end tell
    end if
end tell

The AppleScript app can be found with Spotlight.


In Mountain Lion you have to enable "System Preferences -> Accessibility -> Enable access for assistive device" and change it a little bit to get the AppleScript app solutions running properly:

set myScript to "caffeinate -s rsync -hva --progress --stats --delete /path/to/folder /path/to/folder2"
tell application "System Events"
    if exists (window 1 of process "Terminal") then
        tell application "Terminal"
            reopen
            activate
            set newWindow to do script myScript
        end tell
    else
        tell application "Terminal"
            reopen
            activate
            do script myScript in window 1
        end tell
    end if
end tell

AppleScript with "external" shell script (backup.sh):

This is the final approach the OP is using. Taken from here. In Mavericks (and probably later) you have to allow the AppleScript app to control the computer in "System Preferences -> Security & Privacy -> Privacy -> Accessibility". One can load the full .sh (placed inside the app in Contents/Resources/backup.sh) with:

set bashFile to path to resource "backup.sh"
tell application "Terminal"
    delay 1
    activate
    set newWindow to do script "bash " & quoted form of (POSIX path of bashFile)
end tell

You can refine the AppleScript app by checking if Terminal already has open windows and create a new one to run the shell script:

set bashFile to path to resource "backup.sh"
tell application "System Events"
    if exists (window 1 of process "Terminal") then
        tell application "Terminal"
            delay 1
            set newWindow to do script "bash " & quoted form of (POSIX path of bashFile)
            activate
        end tell
    else
        tell application "Terminal"
            delay 2
            set Window1 to do script "bash " & quoted form of (POSIX path of bashFile)
            activate
        end tell
    end if
end tell
7
  • No easy way to do this without any extra app?
    – Manuel
    Feb 3 '16 at 14:14
  • 1
    @Manuel With your rigid Spotlight prefs I don't see any other possibility. An AppleScript app is the other possibility.
    – klanomath
    Feb 3 '16 at 14:29
  • The “rigid prefs” is logic, I don't need anything else, and I don't want to spam my searches just because I want to be able to launch backup.sh or backup.command. In any case, how would that AppleScript solution work?
    – Manuel
    Feb 3 '16 at 15:14
  • I added activate to bring the Terminal window to the front. If that's not okay, then of course you can delete it. Feb 3 '16 at 16:13
  • @klanomath I will try the AppleScript with the help of this question, and come back ;)
    – Manuel
    Feb 3 '16 at 16:59
0

Run chmod 755 NameOfYourScript from terminal, then ./NameOfYourScript

If you ls it will appear green

2
  • I want to avoid opening from terminal.
    – Manuel
    Feb 4 '16 at 10:08
  • @Manuel its called a shell script for a reason, that will definitely be the easiest way
    – Didaktikos
    Feb 4 '16 at 11:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .