Passing switches to Xcode 3.1 user scripts - xcode

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

Related

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

Change Ruby program into simple Desktop Mac App

I have a program that reads the data in a file, changes certain elements, and creates a new file. The code looks like this:
lines = IO.readlines('single_before_CAP.txt').map do |line|
a = line.split.each { |i| i.capitalize! }
a2 = a.join(" ")
File.open('single_after_CAP.txt', 'w') do |file|
file.puts lines
end
How can I use Xcode and MacRuby to get this program to run as a GUI app? If not Xcode and MacRuby, is there another simple task I can do to make this a standalone GUI app?
One good way to do it:
http://macruby.org/
Sample application:
https://github.com/MacRuby/MacRuby/wiki/Creating-a-simple-application
It sounds like what you want is basically a simple wrapper app that runs your script when double-clicked, rather than a full GUI. Platypus is the easiest way to do that. You just give it your script, set up a couple of parameters (e.g. whether your script wants a file as an argument) and it'll give you a double-clickable app that optionally includes a simple interface such as a progress bar or text output.
If you just want to run a headless ruby application, you can use Automator. Simply create a new workflow, add a line to execute your application, and save it as a ".app". I can give you more specific instructions if you like.
#jmitchell1009
Specific Instructions: If you don't need to input anything to your program (like files or arguments) the simplest way would be to:
Open Automater
Select new "Application" (This will save your final output as a .app, you can choose another option if you want something else)
Select the "Run Shell Script" action, and drag this into your workflow.
Select a shell to run it in (shouldn't matter too much if you're just running a ruby application).
Select where input should go (again, if you have no input, it should matter too much)
Enter the command for your ruby application. Something like "ruby commandpath" etc. (without quotes of course)
You may want to run it a few times to make sure it works. Once you're done, you can save the workflow and it will create a .app for you.
If you have any issues, let me know.

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")

OSX Associate a shell script with a file extension?

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).

Writing GUI frontend for commandline application in Mac OS X

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

Resources