Open windows file share in Finder on Mac via shell script - bash

My Windows-using colleague gave me a url that looks like this:
\\server\share\folder
I'm able to access it on my Mac, via Finder by translating it to:
smb://server/share/folder
Would like to write a little bash script to prepend the smb: and flip the wacky backslashes to forward slashes.
My question is: how to open Finder via shell script? and can I feed Finder a url?

To open the Finder via a shell script on a Mac simply do:
open DIR
... where DIR is the directory you want to open. The Mac OS will open Finder, since that is the correct application for directories. e.g.
open .
... will open the current directory in a Finder window. You should be able to open an smb:// path with that too. The Mac OS should recognize the smb:// prefix and open the appropriate program. If you pass it an http:// path, it'll open using the default web browser.

I was able to accomplish this by making the following shell script which is called via Automator via a keyboard shortcut when the desired Windows path is selected:
OUTPUT=$(echo "$1" | sed 's/\\/\//g')
A="${OUTPUT#*Production}"
foo="smb://path/to/network/directory${A}"
open "$foo"
Here's what it looks like in Automator:

Related

Its possible to change the "webstorm" shell command to open a file in WebStorm?

I open files in WebStorm from the terminal with the command webstorm . when I'm working in a directory.
Is there a way to change the command webstorm to anything more, like wstorm or something shorter to open files fluently?
I'm using macOS.
I Know one way but it's temporary, works untill you close the terminal.
-in your terminal
W=webstorm
then you can use $W in your command instead of webstorm.
Looks like the hint above is very good.
So assuming the shell script "webstorm" is working and your question is how to modify the command AND you want to do it fluently.
Make an alias (your shell may vary, mine is bash)
in the appropriate 'dot' file for your shell
add an alias for webstorm -e like this
alias we='webstorm -e'
now when I want to open a file quickly I type
we filename
And this opens Webstorm in LightEdit mode which opens faster and has a link to access full IDE.
Works like a charm. Also, you could make more than one alias so say ws for the full ide. Enjoy

How to write bash script that will create a shortcut for a mac osx doc?

I am completely new to bash scripting, and would like to know how I can write a script to create a shortcut to go on a mac osx doc.
This is to enable the user to open a specific file by clicking a shortcut on the mac osx doc. In my case, the file in question is .command file that launches an application with the required arguments.
Hope somebody can help.
Thats easy, just dump the .command file into the terminal window and it will give you the path to the file, then
ln -s /Users/username/path/.command ~/Desktop/command
If you double click this the file will open. If you have not set .command to be opened by a given program just use " " and set the program you want it to be opened in.
On macOS, bash scripts do not work right out of the box. You can use shell script instead.
defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>**/Applications/Google Chrome.app**</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'
killall Dock
Make sure that you replace the string /Applications/Google Chrome.app with whatever application you want to put into Dock.
You can just open the terminal app and you can copy paste (with editing the name of the application you want to put into dock) and then killall Dock this should work for you.

Opening Finder from terminal with file selected

I want to open Finder from the terminal with a specific file selected. I know that by using open . I can open the current directory in Finder, but I also want to select some file in the Finder window.
The basic thing I want to do is run a script that randomly selects a file among many in a folder and for that I need to open a new Finder window with the file selected.
The . in your open . command just means path at current location (which would be a folder) so open decides that the correct application to use is Finder. If you were to do open myTextFile.txt which is at your current location in the terminal open will decide to use a text editor instead. You can however specify the application to open the file with by using the -a flag so your command would look like this: open -a Finder myTextFile.txt.
What Faisal suggested will also work, the -R flag is an equivalent to using ⌘↩ (Command Return) in Spotlight.
this and some other nice shell tricks with the open command are described in this post: Shell tricks: the OS X open command
For me, code below works fine.
open -R your-file-path
You can do it like that
osascript -e "tell application \"Finder\"" -e activate -e "reveal POSIX file \"<your file path>\"" -e end tell

On Mac OSX how can I associate a script file with Terminal so double clicking the icon launches Terminal?

I have a python script that I can run in the usual way using Terminal. For example, launch Terminal, cd to the directory, then type ./xxx.py where xxx.py is the name of the file that contains the script.
Now I want to make an icon on the desktop that launches Terminal and runs the python file when I double click it. How do I do that? I thought I could make a shell file with the cd and the launch command and then associate that shell file with Terminal. But I can't seem to associate it with Terminal.
Somebody said to name the shell file with the suffix '.command' but that causes it to launch Flash Builder. I don't know where that associate is set. I can't manually associate Terminal because I can't find it. It isn't in /Applications.
On your Desktop you coul create a file with the following content
#!/bin/bash
python PATH_TO_YOUR_PYTHON_FILE
Then you must make it executable via
chmod u+x FILE_ON_DESKTOP
Alternatively you can create a symbolic link to your python file and make it executable.
You can find Terminal in /Applications/Utilities

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