Writing GUI frontend for commandline application in Mac OS X - user-interface

I am wondering if there is a source of information on how to develop a GUI frontend application for a commandline one in Mac OS X in, but not necessarily, Xcode?
Specifically, I would love a GUI frontend that lets me specify arguments to pass to the commandline program.
Thank you very much.

You have several options. One of the most commonly used programs to create GUI "front ends" to a script is Platypus. However, this does not allow the user to pass arguments like you want. The other option is creating a Cocoa application and using the NSTask class to run your script with arguments that the user specifies in an NSTextField or NSTokenField. In your case, I think creating a full Cocoa application written in Objective-C is overkill.
The most simple way to get what you want and still have it easy is to create a script written in AppleScript. This is a bare bones script that would do the trick:
display dialog "Enter arguments:" default answer "" buttons {"Cancel", "Run"} default button 2
set scriptArguments to text returned of result
do shell script "/path/to/script " & scriptArguments

Check out:
Platypus
iHook

Related

How do I list all window titles of running programs in OS X?

I know how to list all window titles in Windows, for example using Python via the Win32 API. Or eventually I could write it in C/C++ directly.
How do I accomplish this for Mac OS X? It doesn't necessarily have to be in Python, and it doesn't have to be cross-platform. Preferably it would run without requiring any additional downloads (like an applescript file or a bash file using included commands only), but that's not required.
Probably the simplest way of doing this "with no additional downloads" is to use AppleScript:
tell application "System Events"
get name of every window of every process
end tell
Since the Applescript syntax is pretty obtuse, the equivalent Javascript is:
var SE = new Application("System Events");
SE.processes.windows.name()
This will return a structure of the form:
[[], [], ["Stack Overflow"], ["iTunes", "MiniPlayer"], ...]
where each array entry represents one running application, and each string within those arrays represents one window. Empty arrays indicate applications with no open windows.
(Note that this Javascript must be run in Script Editor, not in a web browser. Components of the Scripting Bridge, including System Events, are not available from web browsers.)

How to let osascript not build applescript code which will not hit at runtime?

We have a script to send email using Microsoft outlook or Apple mail application. It will dynamically load the default email from system preference (maybe user input also), and using it to decide which mail client to use.
So the code is as following:
if (mailClientStr contains "outlook")
tell application id "com.microsoft.outlook"
-- <<< there will be error if there is no outlook installed
-- <<< even else branch will be run.
...
end tell
else
tell application id "com.apple.mail"
...
end tell
end if
On an machine which doesn't have outlook installed, and the mailClientStr will be "com.apple.mail", but this script cannot be run by osascript
It complains Can’t get application id "com.microsoft.outlook" even the first branch will not be executed. My understanding is osascript will need to access Outlook apple script interface when load and compile this script (before run it).
I can separate the outlook related code into a separate script, but because there is a lot of data to passing, it will be complex, so I don't want this workaround.
So does there any solution from the apple script language side?
From the AppleScript Language Guide:
Entering Script Information in Raw Format
You can enter double angle brackets, or chevrons («»), directly into a script by typing Option-Backslash and Shift-Option-Backslash. You might want to do this if you’re working on a script that needs to use terminology that isn’t available on your current machine—for example, if you’re working at home and don’t have the latest dictionary for a scriptable application you are developing, but you know the codes for a supported term.
You can also use AppleScript to display the underlying codes for a script, using the following steps:
Create a script using standard terms compiled against an available application or scripting addition.
Save the script as text and quit Script Editor.
Remove the application or scripting addition from the computer.
Open the script again and compile it.
When AppleScript asks you to locate the application or scripting addition, cancel the dialog.
Script Editor can compile the script, but displays chevron format for any terms that rely on a missing dictionary

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.

OS X kernel extension graphical uninstaller

XCode doesn't include uninstallation options for their packager. Generally users simply drag the app to the trash if they want to uninstall it - most apps are self contained in the app package.
However, I have a kernel extension which must be uninstalled using a few simple command lines. There is a script that works, but I am required to provide a graphical uninstaller.
I don't expect there's a plug-n-play example code out there that provides a way to run the script while showing a progress bar, but I'm hoping someone has dealt with this or has a few pointers on how to accomplish this quickly and easily
The script is only two lines with no feedback, so we can execute the commands in the app, as long as we can easily request and use root permissions securely (ie, let OS X handle the permissions - we merely ask for OS X to give them to us which should cause it to ask the user for them similar to how it happens with the package installer) inside the app.
There's a reasonably good approach using a Cocoa-Applescript project in xcode to run a shell script here:
http://www.mactech.com/articles/mactech/Vol.22/22.08/GUI-upyourScript/index.html
It covers using a progress bar, handling errors, and getting the correct root permissions to run the shell script.
Unfortunately it's a bit long to cut and paste here, but the general steps are:
Create new xcode project of type Cocoa-Applescript app
Create and test the intended shell script, add it to your project
Edit the MainMenu.nib to add and name a button(theObject) and progress bar(pMainProgress), then edit title and other aspects of the ui to taste
Tie the button to the applescript in the project (in the Inspector with the button selected check the action box and put in myprojectname.applescript)
Edit the applescript to something akin to the following:
on clicked theObject
-- setup
set myPath to POSIX path of (path to me) as string
-- Start progress bar
set uses threaded animation of progress indicator "pMainProgress" of window "wMain" to true
tell progress indicator "pMainProgress" of window "wMain" to start
-- Do it!
try
do shell script quoted form of myPath & "Contents/Resources/backup.sh" with administrator privileges
on error number 255
display dialog "Sorry, an error occurred. I can't make the copy."
end try
-- Stop progress bar
tell progress indicator "pMainProgress" of window "wMain" to stop
set uses threaded animation of progress indicator "pMainProgress" of window "wMain" to false
end clicked
You can further customize the app (name, for instance) and add text boxes to the window to alert the user what step is happening if you're running multiple scripts (put set the contents of text field "efStatus" of window "wMain" to "Copying files..." in your script after adding a text field to the ui with the name "efStatus")

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