Simple logic in AppleScript -- how do I do it?

I’m currently working on a simple AppleScript Studio app to simplify the printer setup process for the college I work for. Adding an IP printer in OS X is relatively easy, but we just got iPrint for the PCs (click on a link and you’re done), so I want to make it just as easy for the Macs (no server addresses, queue names to remember).

Currently, I have shell scripts that will edit the necessary files to actually create the printers, but I’m having difficulty writing a bit of logic that will simplify the code. I have a box in a window with 13 different check boxes for the 13 printers students can add. I’d like the script to run a loop that looks to see if each box is checked before moving on. If a box is checked, it will run the shell scripts, substituting the proper variables in where the wildcards sit. Then it will move on to the next one, and the next, and so on.

I’ve looked around at various AppleScript resource and what little is out there with respect to loops and it doesn’t make a lot of sense to me. If anyone could give me a hand, I’d be very appreciative.

I’m not sure I follow what you want to do, however, the following code may help. It assumes that you have 13 checkboxes in your main window and that they are named the same as the printers themselves. When the user clicks the “make_printers” button, the script will loop through the printers looking to see if the state of the buttons is 1 (checked), and, if so, run a shell script incorporating the printer name:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Wow! Thanks for the prompt reply. I’ll check it out and let you know how it goes.

OK, I’m close to getting this to work. Here’s what I have so far.


property printerList : {"ellingson", "hillkitt", "hoyme", "kildahl", "larson", "mellby", "mohn", "rand", "thorson", "ytt118", "sc146", "rml115", "om10", "oma13"}
property Username : {"username"}
property UserPassword : {"passw"}


on clicked theObject
	if (name of theObject is "setup") then
		repeat with printerList in the_printers
			if state of button printerName of box "selectionBox" of window "window" is 1 then
				set printerCheck to do shell script "grep " & printerName & "_on_pubprinters.stolaf.edu ~/Desktop/test; echo blank"
			end if
			if printerCheck contains "<Printer " & printerName & "_on_pubprinters.stolaf.edu>" then
				display dialog "" & printerName & " is already added to your list.  Please de-select " & printerName & " and try again."
			else
				do shell script "sudo -s; cat <<EOF >>~/Desktop/test
<Printer " & printerName & "_on_pubprinters.stolaf.edu>
Info " & printerName & " on pubprinters.stolaf.edu
DeviceURI ipp://pubprinters.stolaf.edu/ipp/" & printerName & "
State Idle
Accepting Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
</Printer>
EOF" password "" & UserPassword & "" with administrator privileges
				log "success"
				do shell script "rsync -zIr rsync://viggo.stolaf.edu/lab103/etc/ppd/" & printerName & "_on_pubprinters.stolaf.edu.ppd /etc/cups/ppd/"
			end if
		end repeat
	end if
end clicked

I’m getting hung up on how to define “the_printers”. I have a box within the window (“selectionBox”) that contains all of the printers. Can I reference that? Do I have to define it as a property “the_printers” for it to function correctly? Do I list the printers again? Sorry if this is really easy – I’m still a novice at this. I’ve done tons of looking around but haven’t found anything that deals with properties on this level. Thanks again.

I don’t know about your shell scripts (I can’t test–and there maybe some issues with the hard returns/extra spaces when I copied and pasted from the webpage that I tried to clean so you should make sure they’re OK) but this is closer to what I think you need:

This main issue is that you need to iterate through your list of printers (printerList) using a variable (printerName) that takes on the values of each object in the list as it iterates through the list. Also, why raise an error if the printer already exists? Just ignore it and move on to the next.

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks for that. I made it through the logic part (I think), but I’m having trouble with a “NSReceiverEvaluationScriptError: 4 (1)” error right now. I can’t seem to find what’s tripping it up. I’m sure it’s not the shell scripts, as they run fine outside of the AppleScript.

Apple describes the error in the AppleScript 1.3 release notes:

So it appears as though I’m incorrectly specifying an object. If I try to log “success” at certain events (after printerCheck shell script runs, for example), I get nothing. It appears as though my objects are messing things up from the start. Any idea which could be the culprit?

I’ve worked a few things out, but I’m still getting NSReceiverEvaluationScriptError: 4 (1) when I attempt to add the printers (and actually execute the logic part of the code). Here’s where I’m at now. I have a feeling the problem is that I’m not defining “printerName” but I’m not sure how I would go about doing that.

property printerList : {"ellingson", "hillkitt", "hoyme", "kildahl", "larson", "mellby", "mohn", "rand", "thorson", "ytt118", "sc146", "rml115", "om10", "oma13"}
property Username : {"username"}
property UserPassword : {"passw"}

on clicked theObject
	if (name of theObject is "setup") then
		
		repeat with printerName in printerList
			if state of button printerName of box "selectionBox" of window "window" is 1 then
				set printerCheck to do shell script "grep " & printerName & "_on_pubprinters.stolaf.edu ~/Desktop/test; echo blank"
			end if
			log "success"
			if printerCheck contains "<Printer " & printerName & "_on_pubprinters.stolaf.edu>" then
				display dialog "" & printerName & " is already added to your list.  Please de-select " & printerName & " and try again."
			else
				do shell script "sudo -s; cat <<EOF >>~/Desktop/test
<Printer " & printerName & "_on_pubprinters.stolaf.edu>
Info " & printerName & " on pubprinters.stolaf.edu
DeviceURI ipp://pubprinters.stolaf.edu/ipp/" & printerName & "
State Idle
Accepting Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
</Printer>
EOF" password "" & UserPassword & "" with administrator privileges
				log "success"
				do shell script "rsync -zIr rsync://viggo.stolaf.edu/lab103/etc/ppd/" & printerName & "_on_pubprinters.stolaf.edu.ppd /etc/cups/ppd/"
			end if
		end repeat
	end if
end clicked

on action theObject
	(*Add your script here.*)
end action

When you get this type of error, it is that you are referencing an object incorrectly. Simply posting the script won’t really help. You need to upload you project files someplace publicly–including the nib–so we can see how you’ve constructed the objects to see why your references are failing. Barring that, we can be of little more help.

Jon

Sorry about that. I appreciate all the help. Here’s a link to the whole project – everything I have to date.

http://www.stolaf.edu/people/dechant/PrintSetupUtility.zip

I get a 404 error on the link.

Jon

Sorry about that. I edited the link above.

See, that was helpful. In your nib, there was no button named “oma13” so referencing it caused an error. Looking at your script, I simplified it. Why raise a dialog if the printer already exists? Just skip it and go to the next. I also cleaned up the logic a bit to make sure printerCheck is actually defined (in the original script, you run the risk of not having this variable defined in your script when you need it later):

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]