OSX Associate a shell script with a file extension? - bash

I want to associate the .exe file extension with a shell script that launches wine. What is the best way to do this?
From what I've gathered, I need to create an AppleScript that will call wine, but how do I get the name of the input file in the AppleScript? If there is a better way to do this, let me know, but as far as I know this is the best way.

You can also use Automator to wrap your own bash, python or ruby script in an "Application".
Open Automator and choose to create an Application.
Find the Action "Run Shell Script" and double-click it or drag it to the script area.
Select the interpreter you want (bash, other shells, python or ruby).
Set the "Pass input" option to "as arguments". (In the automator model, the application "receives files and folders as input"; hence this lets your script see filenames as commandline arguments).
Enter your shell script in the edit area. On bash, use "$#" for the list of commandline arguments (individually quoted to protect embedded spaces).
You can now save the script (it will get a .app extension), and move it to the Applications folder or other reasonable location. It can be associated with a file type, like any other application.
NOTE: This works on Mountain Lion (10.8); can someone comment on how long Automator has been able to do this?

You can use an AppleScript application to do this - files are passed to the open handler, the same as a droplet, for example:
on open theFiles
set arguments to ""
repeat with anItem in theFiles
set arguments to arguments & space & (quoted form of POSIX path of anItem)
end repeat
do shell script "/path/to/wine" & arguments
end open
To associate a particular document type with your application, you will first need to add information to your application's information property list (Info.plist) to tell Launch Services what types of documents it can handle. See Apple's Document-Based Applications Overview (or take a look at the settings in other applications).

Related

Programmatically building applications for "Open with..." on Mac

I'd like to create a program which uses certain information given by my program's users and bakes this info into executables which can be used with Mac's "Open with...".
Since bash scripts cannot be used directly for Mac's "Open with..." functionality1, however, I'd like to find a way to wrap such a script (and as painlessly as possible).
I am aware Automator lets one build applications which include bash scripts and which work with "Open with...", but I want to be able to programmatically build both the script and the containing application (and I haven't found that Automator can accept command line arguments to compose applications).
What are some simple, lowest common denominator/open format approaches (without using Python, etc.) whereby I can do this (ideally in a way that could work on Linux as well)?
I found I was able to build an AppleScript application programmatically using osacompile (or apparently also possibly using Apple's JavaScript) using on open listener to listen for "Open with...", default file association, or dragged files (including when placed on the dock) and an on run listener to work with command line invocation. I could then use do shell script internally to invoke my desired file (in my case, calling the Node.js binary with a Node script and some arguments).
I was also able to do try/on error check for an input and if none were present do a choose file with prompt (specifically choose file with prompt "Prompt message" of type {"js"} for JS files) so that the application could be double-clicked without a file argument but yet trigger a file dialog (e.g., for use on the dock).

Apple Automator take screen capture and save to user-specified location

So, I'm trying to use Automator under Mac OS Yosemite to create a service to allow a user to take a screenshot and save it to a location they specify, through some sort of "Save As" dialog. It seemed like it should be easy, but for some reason I'm running into difficulty with it. The screenshot component is easy, using the "Take Screenshot" action in Automator, but it's the saving it to a custom location that's causing me problems.
After trying a few different approaches, it seemed the easiest thing to do was to save the screenshot to a fixed directory/filename from within the "Take Screenshot" action, and then (using AppleScript) rename it in that directory, and move it to the user-specified target directory. So, I added a "Run AppleScript" action to my service. In it, I generate the dialog to choose a file name/path, using the choose file name command in AppleScript. I'm trying to split up the file name from the path, so that I can rename the file I save in "Take Screenshot," and then move it to the path that I'd like to save it at. I can get the full path, but am having problems just getting the filename from the path—and I've tried a variety of suggestions from what I've seen online. In my screenshot, the error shown was from attempting to do
I'm not set by any means on this flow, so if anyone has any better suggestions on how to do what I'm trying to do, by all means please let me know. Otherwise, if someone's able to just tell me how I can extract the filename from the path (and also if there's some special way you have to use that string to rename the file) that'd be great!
AppleScript code pictured in screenshot:
on run {parameters}
set thePath to (choose file name with prompt "Where would you like to save your file?")
tell application "Finder"
display dialog thePath as string
end tell
set UnixPath to POSIX path of (thePath as text)
display dialog UnixPath
end run
I tried this but it didn't work:
set basePath to POSIX path of (parent of (thePath) as string)
Thanks for checking it out!
An easy way would be to use the command line tool "screencapture". It has many options you can choose. See its man page. Here's an example that you can run as an applescript directly or you could put this inside an applescript automator action if you want.
Good luck.
set thePath to (choose file name with prompt "Where would you like to save your file?")
do shell script "screencapture -mx -T1 " & quoted form of (POSIX path of thePath & ".png")
I've created a Folder action in Automator. Choose Desktop. Add Find Finder objects (search: Desktop). Add Move Finder Object and choose your preferred destination. This will automatically move all your screenshots.

easiest way to create free standing Mac OS X perl applications

This may be a basic question. I have written a small perl script to run on a Mac running OS X which can be called from the finder by double clicking in the normal way. The file is executable and contains starts with #!/usr/bin/perl and input and output is via the clipboard. This all works but automatically opens a terminal window which the user must then close once execution is finished.
Is there an easy way to run this program as an application without opening a terminal? Can one do this with the native OS X perl? Or do I need to download something? Since the program will also be used by other users, the simpler the solution to better.
With the application "AppleScript Editor" : open it
Copy/paste this script
tell me to path to resource "this Name.pl" in directory "Scripts"
do shell script (quoted form of POSIX path of the result)
In the first line, change the name "this Name.pl" by the name of your perl file.
Save as --> Application
In the Finder :
Copy your perl file (executable) to the folder "/Contents/Resources/Scripts" of the created application
For Win32/Linux/or Mac use http://www.cavapackager.com/
If you're distributing your application to others, Platypus includes an installer to build free-standing apps around scripts.
Platypus supports Perl, Python, PHP, Ruby, Swift, Expect, Tcl, AppleScript or any other user-specified interpreter. It is free, open-source software distributed under the terms of the three-clause BSD license. It can run silently (without opening a terminal window, as you require), or can display graphical feedback of script execution as progress bar, text window with script output, droplet, WebKit HTML rendering or status item menu.

AppleScript Transmit Script for uploading file to replace itself on web server

So I have this idea for a handy little AppleScript which in my opinion would be very handy in speeding up the process of uploading a local file, to its same location on the server.
In other words, you have to specify the home folder on the server and locally, but once that's finished, it would be nice to just press like "Command" + "Shift" "U" for upload or some other hot key combination not in use by OS X for uploading the currently selected file in the Finder.
I find myself needing to do this a lot, and it will save a lot of time!
Someone please tell me if there is an easier way to do this, but I think this will be a good learning experience on top of it all.
I need some help on how I should get started however..
1) the command line program curl can upload files. 2) if you have a file selected in a Finder window applescript can get the selection. Using those 2 ideas you can automate your task. I don't know the exact curl command but that should be easy to find using google. So you select a file in the Finder and then run the script. The script can be run with a keyboard shortcut as you mentioned or just put it in the Script menu and run it from there.
tell application "Finder"
set selectedFile to item 1 of (get selection)
set selectedFile to selectedFile as text
end tell
do shell script "curl -switchesToUpload " & quoted form of POSIX path of selectedFile
I use Cyberduck which is an ftp client you can set it up so when you double click a file on the server it opens it up in your favorite editor.( textmate is my favorite.) it atuomagiclly downloads and uploads when you save.
this seems like a much better solution to the problem

Passing switches to Xcode 3.1 user scripts

I have a user script that would be much more useful if it could dynamically change some of its execution dependent on what the user wanted. Passing simple switches would easily solve this problem but I don't see any way to do it.
I also tried embedding a keyword in the script name, but Xcode copies the script to a guid-looking filename before execution, so that won't work either.
So does anyone know of a way to call a user script with some sort of argument? (other that the normal %%%var%%% variables)
EDIT:
User scripts are accessible via the script menu in Xcode's menubar (between the Window and Help menus). My question is not about "run script" build phase scripts. My apologies for leaving that somewhat ambiguous.
You can't pass parameters to user scripts — instead, user scripts operate on the context you're working in (e.g. the selected file, the selected text, etc.).
You should use the context to determine what the user really wants.
User scripts are accessible via the script menu in Xcode's menubar (between the Window and Help menus). Wasn't sure what else to call them. What I'm asking about are not "run script" build phase scripts.
I suppose you could do something like this:
#!/bin/bash
result=$( osascript << END
tell app "System Events"
set a to display dialog "What shall be the result?" default answer ""
end tell
return text returned of a
END
)
# do stuff with $result
There are built in utility scripts that allow you to prompt the user and capture the reply.
You could prompt for a string, for example, then based on that perform a certain task.
The String prompt is:
STRING = `%%%{PBXUtilityScriptsPath}%%%/AskUserForStringDialog "DefaultString" "DefaultWindowName"`
If you notice, you're just calling an applescript they wrote using a static path. You could write your own applescript dialog and place it there if you want and bypass the need for cumbersome osascript syntax. There are others (for files, folders, applications, etc)
User Scripts Documenation

Resources