Launch a bundle file with apple script - applescript

I have a bundle file on osx with a .stack extension. eg test.stack.
Normally a user will double click this file, and that launches a helper app "StackInstaller" momenterally, that copies the .stack file to another pre-determined folder(same folder every time).
I would like to script this with apple script if possible. But i aint big on apple script and my first attempts are failing.
eg:
tell application "Finder" open file "test.stack" of [pathtomydesktophere]
another attempt :
tell application "StackInstaller" open file "test.stack" of [pathtomydesktophere]
Really i want to simulate a user double clicking the .stack file, and i wrongly assumed thats what the open command was doing.
Kind regards Gary

Do a shell script: open ~/Desktop/test.stack

Related

Why do my script paths get changed for my script app when I access it through the Finder's Script Menu?

Why do my script paths get changed for my script app when I access it through the Finder's Script Menu and BBEdit is the frontmost application?
FWIW, the change happens because I access an external script .scptd file within my main script.
One other item, the actual app in the global Finder Script Menu is an alias.
If I run my script within Script Editor, no change in paths - regardless of BBEdit.
If I run my script app alias by double clicking on it in any folder, even the global Finder's Script Folder, no change in paths regardless of BBEdit.
If BBEdit is in the background, no change in paths.
Only when BBEdit is frontmost and I click on the Finder's Script Menu and select my app, then the script app produces an error that says the following about my external .scptd file:
Macintosh HD:Users:johnlove:Library:my_Project:Project_Support:Scripts:External_Script_Libraries:externalLibrary.scptd
... and I definitely did not place the original or any of these folders/sub-folders in my Library folder.
???

How to Notarize an executable shell script (.command) file for MacOS 10.15 Catalina

This is a DIFFERENT scenario than discussed in other postings:
I have a .sh (shell script) which I've changed to a .command (executable shell script). It's signed using code sign, and being distributed on a .dmg that is signed.
However, Catalina (of course) doesn't like it -- complains it "can't be opened because Apple can't check it for malicious software". The user can still open the .command file if they right-click and select "Open" from the context menu, but the alert is still displayed, although this time with an available Open button.
I'd really like to avoid having to create a simple command line APPLICATION just for the purpose of executing a few shell script lines -- just so it can be notarized.
Anyone know of how to get around this?
Stephen
Your going to hate this but throw it into a .app bundle and call it from the .plist as the exec. Then you may proceed with notarization.

Run script in ".app" (Application Bundle) as the primary executable

I'm trying to use Mono to make a portable application that works on Windows, Mac, and Linux, but I want to have a standard DMG file that allows users to simply click and drag my application to their Applications folder on their Mac. In the best answer to this post I found that by creating an executable script beginning with #! in the MyAppName.app/Contents/MacOSX/ folder, it could run whatever I liked.
How do I get a script to actually run?
I made a blank "Hello World" Cocoa App in XCode, and copied the .app file to the Applications directory, and everything worked great. But when I replaced the executable in the MacOSX folder with a script starting with #! it didn't run the script.
I tried chmod 755 myscript, I tried deleting the old executable file and renaming my script to be the old filename, but all that served to do was stop the app from running at all. The permissions of the script, and the filename, are both correct.
So then I assumed that maybe scripts wouldn't work, and XCoded a command line utility, again that would simply spout "Hello World!", but still it didn't run.
The ".app" extension is formally called an Application Bundle, and they have some pretty good documentation, but none of it seems to tell me why my approach isn't working.
Here is the .app bundle that I'm working on. Inside the MasOSX directory you'll find hello (the Hello Word command line utility) and script (a one line script with a #! as the first line) and XCodeTest (a Hello World cocoa app).
Scripts in Unix
Well, first of all, the correct syntax for the first line of a script is
#! <path-to-script-interpreter> [<optional-args>]
For instance
#! /usr/bin/perl -wT
... perl code here
Using the open command
However, this won't help you because the open command does not support shell scripts as launchers. You need to write some C / Objective-C code and compile it into a native executable. Simply using system() or execv() inside that tool won't work either, you'd have to dlopen() the Mono runtime.
Look at monodevelop/mainbuild/MacOSX/monostub.m for an example. This is what MonoDevelop uses to launch MonoMac application.
When you write a Cocoa app with XCode, it will compile your app into native code and put the resulting binary as launcher in the Contents/MacOS directory.
The easy solution
Why don't you simply create a MonoMac application with MonoDevelop? This would automatically create the .app package for you - no need to fiddle around with scripts or custom launchers.

Making an executable bash file run when clicked

I have a bash file that does some file manipulation. I don't want to have to open the terminal every time I run it. Is there a way to make the program run when I double click it? (Like a windows .exe file)
Thanks
You can add a ".command" extension to the filename -- then double-clicking it will automatically open Terminal and run the script in a new window. Note: this assumes you still want to watch/interact with the script via a Terminal interface; if you want to avoid this as well, wrapping the script with Platypus, AppleScript, or Automator (as Zifei and Ned suggest) would be better options.
What you need is Platypus.
Platypus is a developer tool for the Mac OS X operating system. It can be used to create native, flawlessly integrated Mac OS X applications from interpreted scripts such as shell scripts or Perl and Python programs. This is done by wrapping the script in an application bundle directory structure along with an executable binary that runs the script.
The easiest thing to do is to type: sudo chmod 755 the_file_Name.This will allow you to double click on the file in the finder.
With OS X 10.5+, you can wrap the bash shell script in an AppleScript application using the AppleScript editor or an Automator application using Automator.app (see the Automator on-line help).
You could write (and there are apps out there that do this) an OS X app that accepts arbitrary .sh files and executes them. However, that's generally a bad idea as it could open you up to attacks if you inadvertently download a malicious shell script file that is automatically opened by your web browser. Better to be explicit.

Executing Shell Scripts from the OS X Dock?

How do I set up a shell script to execute from the Mac OSX dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing?
You could create a Automator workflow with a single step - "Run Shell Script"
Then File > Save As, and change the File Format to "Application". When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.
The benefit to this is it's really simple to do, and you can very easily get user input (say, selecting a bunch of files), then pass it to the input of the shell script (either to stdin, or as arguments).
(Automator is in your /Applications folder!)
If you don't need a Terminal window, you can make any executable file an Application just by creating a shell script Example and moving it to the filename Example.app/Contents/MacOS/Example. You can place this new application in your dock like any other, and execute it with a click.
NOTE: the name of the app must exactly match the script name. So the top level directory has to be Example.app and the script in the Contents/MacOS subdirectory must be named Example, and the script must be executable.
If you do need to have the terminal window displayed, I don't have a simple solution. You could probably do something with Applescript, but that's not very clean.
On OSX Mavericks:
Create your shell script.
Make your shell script executable:
chmod +x your-shell-script.sh
Rename your script to have a .app suffix:
mv your-shell-script.sh your-shell-script.app
Drag the script to the OSX dock.
Rename your script back to a .sh suffix:
mv your-shell-script.app your-shell-script.sh
Right-click the file in Finder, and click the "Get Info" option.
At the bottom of the window, set the shell script to open with the terminal.
Now when you click on the script in the dock, A terminal window will pop up and execute your script.
Bonus: To get the terminal to close when your script has completed, add exit 0 to the end and change the terminal settings to "close the shell if exited cleanly" like it says to do in this SO answer.
I know this is old but in case it is helpful to others:
If you need to run a script and want the terminal to pop up so you can see the results you can do like Abyss Knight said and change the extension to .command. If you double click on it it will open a terminal window and run.
I however needed this to run from automator or appleScript. So to get this to open a new terminal the command I ran from "run shell script" was "open myShellScript.command" and it opened in a new terminal.
As long as your script is executable and doesn't have any extension you can drag it as-is to the right side (Document side) of the Dock and it will run in a terminal window when clicked instead of opening an editor.
If you want to have an extension (like foo.sh), you can go to the file info window in Finder and change the default application for that particular script from whatever it is (TextEdit, TextMate, whatever default is set on your computer for .sh files) to Terminal. It will then just execute instead of opening in a text editor. Again, you will have to drag it to the right side of the Dock.
In the Script Editor:
do shell script "/full/path/to/your/script -with 'all desired args'"
Save as an application bundle.
As long as all you want to do is get the effect of the script, this will work fine. You won't see STDOUT or STDERR.
I think this thread may be helpful: http://forums.macosxhints.com/archive/index.php/t-70973.html
To paraphrase, you can rename it with the .command extension or create an AppleScript to run the shell.
As joe mentioned, creating the shell script and then creating an applescript script to call the shell script, will accomplish this, and is quite handy.
Shell Script
Create your shell script in your favorite text editor, for example:
mono "/Volumes/Media/~Users/me/Software/keepass/keepass.exe"
(this runs the w32 executable, using the mono framework)
Save shell script, for my example "StartKeepass.sh"
Apple Script
Open AppleScript Editor, and call the shell script
do shell script "sh /Volumes/Media/~Users/me/Software/StartKeepass.sh" user name "<enter username here>" password "<Enter password here>" with administrator privileges
do shell script - applescript command to call external shell commands
"sh ...." - this is your shell script (full path) created in step one (you can also run direct commands, I could omit the shell script and just run my mono command here)
user name - declares to applescript you want to run the command as a specific user
"<enter username here> - replace with your username (keeping quotes) ex "josh"
password - declares to applescript your password
"<enter password here>" - replace with your password (keeping quotes) ex "mypass"
with administrative privileges - declares you want to run as an admin
Create Your .APP
save your applescript as filename.scpt, in my case RunKeepass.scpt
save as... your applescript and change the file format to application, resulting in RunKeepass.app in my case
Copy your app file to your apps folder
Exact steps to achieve that in macOS Monterey 12.3
Open Automator
File -> New
Choose Application
Go to Library -> Utilities
Double-click Run Shell Script
Type in whatever command you want to run. For example, try the command to toggle Dark Mode:
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to not dark mode'
File -> Save
Drag the saved file to the Dock, done!
pip install mac-appify
I had trouble with the accepted solution but this command worked for me.
Install
pip install mac-appify
Run
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/appify ~/bin/webex_start.sh ~/Desktop/webex.app
Adding to Cahan's clear answer ... to open a shell script from the dock without passing any arguments to it, try:
open [name of your script].scpt"
example:
open "//Users/user/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/myScript.scpt"
Someone wrote...
I just set all files that end in ".sh" to open with Terminal. It works
fine and you don't have to change the name of each shell script you
want to run.

Resources