How to smoothly make changes to Snow Leopard via bash script - macos

I have a bash script that I run to toggle visibility for my desktop icons which is the following:
#!/bin/bash
DESKTOP=$(defaults read com.apple.finder "CreateDesktop")
if [ $DESKTOP == 1 ]
then
defaults write com.apple.finder CreateDesktop -bool false
else
defaults write com.apple.finder CreateDesktop -bool true
fi
killall Finder
it works, but there's 2 issues here I don't know how if it's possible to do using bash.
when running this script it It opens up terminal run the app and leave the terminal opened.
Is there any way to make this script run without have to open the terminal?
I've found this link Making an executable bash file run when clicked which use Platypus as a wrapper for your script, but I would like to know if it's possible to do something natively without any extra tool :)
when running the script the killall Finder makes everything restart so the screen blinks and you lose the actual state of your windows
Is is possible to make only the desktop icons fade-in/out smoothly without affecting windows and other resources based on Finder?
this Camouflage app works like this, so basically I would like to know if it's possible to replicate this behavior using bash script.
Well that's it, I hope it's easy to understand.
You could say that I could use this camouflage app for my needs but actually I'm trying to learn a bit about it and trying to do it myself (and obviously, with your help :)
Thanks in advance
Update
I was having a look at the second issue and I'm not sure but it seems that app in question doesn't hide the icons from destkop but instead, it creates an overlay between the most front level on the desktop(that is over the icons) and generate an image which is the same used as a desktop image. hence the fading effect, since it can control the image transition when appearing. what helped me analyze this was since every time you close the app the icons appear back. So it must have something do to with it.
If I am wrong the it's really possible to do other way, please share your opinion :)

Not sure about a solution for your second question, but for the first: take a look at the third answer for the StackOverflow question you linked to:
Making an executable bash file run when clicked
You can create an application bundle with AppleScript Editor or Automator that will run your script without opening a Terminal window.

Related

Mac OS X app running shell script but no terminal window

I am fairly new to Mac OSX, and am trying to create an .app file to run in the Applications folder. I'm using MacOS Big Sur, and this will just be run on a Mac (it doesn't need to be cross platform). There is a jar file that executes by running a shell script, as well as a few extra resource files, so ultimately I'd like to bundle this all together in something like a dmg so that I can share it easily with a few other people.
I followed the advice given here and here to set it all up, and almost everything works. The program starts when I double click on the .app file, but without a terminal window. Unfortunately I need the terminal window to open because I use it to log messages to the user.
Terminal is the default app for the shell script, and a terminal does open when I run the shell script directly by double-clicking on it. The script file works with an .sh extension and without one, though I get an error trying to run the .app if the script has an .sh extension. Everything has execute permissions. I went through the Info.plist docs but couldn't find anything about the Terminal. I also tried creating the .app with Automator, but with the same result.
Any suggestions would be very much appreciated, as at the moment I'm completely stuck. As I said, ultimately I want to have a way of sharing this with others who may not be very computer-savvy (e.g. they're used to just downloading things from the App Store and wouldn't be able to install things using the command line). So if I'm going about this all wrong or there's an easier way, then let me know that too.
Unfortunately I need the terminal window to open because I use it to log messages to the user.
If this is all you want the Terminal app for then you don’t need it all.
The Terminal app is a GUI app which runs a shell using standard OS calls, passes keyboard input to that shell (and hence any commands it in turns invokes) via a pipe, and reads the output of the shell (and hence...) and displays it in a window.
You can run your shell script direct from your own app, collect the output, and dimply that output in a window in your app.
In Objective-C the classes you want to look up are NSTask, to run a shell passing it your shell script, and NSPipe, to create pipes needed.
There are plenty of Q & A’s on SO about NSTask/NSPipe, here is one and here is another which uses Swift.
Note that both of the above read all of the output before converting it to a string which can then be displayed in a window or otherwise processed. This is not required and if you have a long running shell script and wish to display output as it runs you can read shorter chunks from the pipe. Read the documentation to see how to do this.
I'm posting my solution in case it helps anyone in the future. As the comments/answers said, what I really needed to know was how to get my app to open a terminal window. Obviously by creating the app manually (creating the folder structure and minimal Info.plist) I was missing some key elements.
I tried to generate one using Xcode. I'm sure it's pretty straightforward, but I got bogged down trying to work out the Swift code.
What worked for me was creating an AppleScript using Script Editor. The script simply tells the Terminal program to run my bash script:
tell application "Terminal"
do script "/Applications/{name of app}/Contents/MacOS/run.sh;exit"
end tell
The key is that Script Editor can save this as an app to the Applications folder, which means it creates the necessary folder structure and files. After that I could just copy my program files into the MacOS folder, which is where my bash script looks for everything.
One option might be to give the script file the extension .command, e.g., and then open that, e.g.:
open myscript.command
The myscript.command file needs execute permissions (chmod a+x myscript.command).
These .command files can also be double-clicked in Finder to execute them in a new Terminal window.

Mac OSX bring the console window front most with shell command

Here I want to ask you to help to bring the console window top most with a shell command. Is it possible?
there was a script execution in my work environment, and will take about several minutes to complete, I will move to other work while running, it is better there are some tips to notify me when script completes run.
So I think bring the window top most is the most direct way.
Please tell me how to achieve this, I'm working on windows, new to MacOS.
Thanks,
Levi
I don't know how to bring the console window to the front, but if you're working in the terminal, you could do something like:
$ path/to/script ; tput bel
or
$ path/to/script ; say "script 1 is done"
Where path/to/script is whatever your usual method of loading the script is.
I recommend the first method, as it flashes the screen, makes a beeping noise, and causes the terminal icon to bounce up and down on my dock in OS X 10.10. The second might be better if you need to figure out which script is done from among a bunch of scripts. If you have coworkers who you don't want to disturb, you can change the preferences in terminal so that an alert only flashes the screen and doesn't also make a noise.
If you have access to the source code of the script, you can also add tput bel or say "command" to the last line of the script and you'll get the same behvaior.

Launching a Shell Script using a Global Shortcut on OSX Snow Leopard

I have a shell script, which I launch several times during work everyday. I do this by launching terminal, and launching the script from within. However, I would like to be able to launch it (from within a terminal) with a global OSX keyboard shortcut (Cmd+Shift+R say).
There are quite a few applications which work in this manner already (for eg. EverNote, RemindMeLater, even the default Cmd-Space which brings up the search utility) and hence, I am guessing this should be at least theoretically possible.
Could someone please tell me if and how this can be done?
Instead of using a global shortcut key, maybe try creating an applescript that runs the shell script? So just open at AppleScript Editor, and enter something like this.
do shell script "//Your script";
Then save it as an application in the format drop down.
After that, you can run the script just by opening up the application with something like spotlight.

Putting the display to sleep (⇧⌃⏏/shift+control+eject) in AppleScript

Is it possible to write an AppleScript to put the display to sleep (which locks the display if the computer is set to lock on sleep)? You can do this from the keyboard by entering ⌃⇧⏏ (shift+control+eject); this leaves all the programs, etc., running, and just turns off the screen.
I've been wanting to do this for a while now. I just found out how in the man pages. You can use the following command to achieve instant display sleep. (I have tested it on OS X 10.10.)
pmset displaysleepnow
(no root privileges / sudo required!)
I'm not sure if this works for 10.9.4 yet but by all means give it a shot!
you can use: tell application "Finder" to sleep
Or use bettertouchtool. It is a small app which you can use native functions in addition to applescript.
do shell script "pmset displaysleepnow"
Edit 2015-08-23: This is possible (from the shell) as of OS X 10.9! Go see user3064009's answer for the update :-)
There's no good way to do this; there's a SuperUser question about this same thing. Depending on why you want this, however, there's one workaround I know: activate the screen saver. (This is what they suggest over on SuperUser). You can do this in AppleScript with launch application id "com.apple.ScreenSaver.Engine", or from the command line by running the application /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine. I don't know whether or not this is technically documented anywhere, but it's worked for several iterations of the OS now. It may not do exactly what you want—your screen saver may, for instance, be colorful, which isn't helpful if you want a black screen—but it will be the same in that it does lock the screen if you have that set up.
For a nice catalog of other workarounds, check out this MacScripter thread: it documents that
There's no scriptable way to do this.
You can't tell AppleScript to key code EJECT, as there's no such key code.
You can use pmset to tell the display to go to sleep in one minute, but then you have to wait.
There's an undocumented IOKit way to do this; there's a mailing list post explaining how.
I haven't found an easy way to do it programmatically, but I did find a very small, free app that puts the display immediately to sleep, called "SleepDisplay." (There is another app of the same name that did NOT work for me.)
So you can just
tell application "SleepDisplay" to activate
Link: http://www.macupdate.com/app/mac/26234/sleep-display

open console (terminal) window and execute command (rsync) on os x

I am from a windows background and trying to help a mac user friend to backup her pictures, docs, etc. onto an external drive. In windows, I would accomplish this by creating a simple batch file with an xcopy command and have a shortcut on the desktop that pointed to that .bat file when double clicked. However, in the mac world I am having significant trouble finding how to do this. I have searched repeatedly to find the mac equivalent, but all I find are sites saying things like "there are so many options on a mac - use one of them." However, none have ever given a specific solution nor pointed to a specific solution. Anyone here know of a specific step by step process to accomplish this? I simply want to be able to have her double click an icon on the desktop and have it copy her personal documents (not application settings or other overhead) to her external hard drive. Any help would be appreciated.
Create the batch file, which is usually called a shell script.
Enter all the commands that you want to run.
Set the executable bit, this is done with chmod +x path-to-the-file in Terminal.
Show info for the script and set Terminal to the application which should open it.
However, what I've done in similar situations and that I would recommend that you do is that I've created a shell script and instead of using Terminal I've initiated it from an AppleScript application. You can of course embed the entire shell script in the AppleScript as well. Basically it will look something like the following:
on run
do shell script "rsync -av ~/Pictures /Volume/Backup"
end run
Repeat the do shell ... line for each folder that you want to copy, or call the shell script itself. Then use AppleScript Editor which is included with Mac OS X and save it as an actual application.

Resources