noob dilemma w/ interface builder & applescript

i’m VERY new to Applescript and am trying to build a simple application for use at work.
the following interface was built in interface builder. see that switch button that says “Digital Ad”?

Well, Upon entering all of the text fields and hitting the “Auto Create” button, my intent is to create a series of folders on a server with folder names based on the info entered in the text fields.

Here’s my problem:
if a user checks the “Digital Ads” box, upon hitting “Auto Create” i would like an extra folder to be created. However, i have so far been unable to figure a way to recognize whether the box is checked or unchecked, and then to specify what and when to do what i want to do (which is to create an extra folder).

Below is an abridged version of my verbose and inefficient script. Amy ideas or places to point me in the right direction?

p.s. ANY help would be appreciated. this forum so far seems to be a very valuable source of information! Thanks, Josh

global myJob, myDate, myCustomer, myFolder, myY, myz, mysupport, myworkup, myselection, mynewjob, mywidth, myheight, mysize, myDigital

on clicked theObject

if name of theObject is "thereset" then
	tell window "Sherpa v1.0.5"
		delete contents of text field "thejob" of tab view item "tabnew" of tab view "maintab"
		delete contents of text field "thedate" of tab view item "tabnew" of tab view "maintab"
		delete contents of text field "thecustomer" of tab view item "tabnew" of tab view "maintab"
	end tell
else if name of theObject is "create" then
	tell window "Sherpa v1.0.5"
			set myJob to contents of text field "thejob" of tab view item "tabnew" of tab view "maintab" as string
			set myDate to contents of text field "thedate" of tab view item "tabnew" of tab view "maintab" as string
			set myCustomer to contents of text field "thecustomer" of tab view item "tabnew" of tab view "maintab" as string
			set myFolder to contents of text field "thejob" of tab view item "tabnew" of tab view "maintab" & " " & contents of text field "thedate" of tab view item "tabnew" of tab view "maintab" & " " & contents of text field "thecustomer" of tab view item "tabnew" of tab view "maintab" as string
			set myY to "SUPPORT"
			set myz to "WORKUPS"
			set mysupport to contents of text field "thejob" of tab view item "tabnew" of tab view "maintab" & " " & myY as string
			set myworkup to contents of text field "thejob" of tab view item "tabnew" of tab view "maintab" & " " & myz as string
		on error
			display dialog "Sherpa has encountered an error."
		end try
		display dialog "Create job: " & myJob & " " & myDate & " " & myCustomer & " ?"
	end tell
	tell application "Finder"
		--compound if statements for june
		if myDate is equal to "6-1" then
			set myselection to "Ad_Server:*ADS:06 JUNE 04:01 JUN TUE" as string
		else if myDate is equal to "6-2" then
			set myselection to "Ad_Server:*ADS:06 JUNE 04:02 JUN WED" as string

–this compound IF statement continues and includes each date for the rest of the year…

			display dialog "please enter date correctly. ex. 6-1 or 12-23."
		end if
		set mynewjob to myselection & ":" & myFolder as string
			make new folder at myselection with properties {name:myFolder}
		end try
			make new folder at folder mynewjob with properties {name:mysupport}
			make new folder at folder mynewjob with properties {name:myworkup}
			open folder mynewjob
		on error
			display dialog "Error. Either there is no connection to the Ad Server or the job folder already exists."
		end try
	end tell	
end if

end clicked

on should quit after last window closed theObject
return true
end should quit after last window closed

You can see if a checkbox is checked by looking at its state, 1 for checked, 0 for not checked:

Similarly, when you want to reset the window, you set the checked, unchecked state of the button by setting the state to either 0 or 1:


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

Thanks! I’ll go and try that and see if it works.


Thanks so much for the prompt and accurate help! I’m one step closer to finiishing this applescript now!


Incidentally, your “on clicked” code could be condensed down significantly (actually, even more than what I’ve done here but this should give you some ideas):


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

again, thank you. i’ll be going through the script this afternoon making those changes. on a side note, is there a good online resource for writing applescripts for QuarkXpress 4? i haven’t had much luck short of a few sites with example scripts.


Try the search engine on this board. There are lots of good examples.


thanks. will do.

Greetings. In your code you write…

You could (and should) write some code that will dynamically write these strings for you, otherwise you’ll be writing 365.25 different entries, all of which will have to be rewritten every year to account for the changing of the weekdays. :cry:

I started on some code that will write this string for you dynamically. I tested for the single digit day number and corrected to double digit (i.e. “4” → “04”). I also wrote a subroutine that changes text months to numeric months, which could be modified to be used in other translations.

Using this code will also accept many different input formats (i.e. “6/24/04” or “june 15”), and will automatically make sense of it for you…taking some burden off of you to handle error-checking.

(* Identify the months' numeric values *)
property monthsAsNums : {{theMonth:"January", theNum:"01"}, {theMonth:"February", theNum:"02"}, {theMonth:"March", theNum:"03"}, {theMonth:"April", theNum:"04"}, {theMonth:"May", theNum:"05"}, {theMonth:"June", theNum:"06"}, {theMonth:"July", theNum:"07"}}

on clicked theObject
	if name of theObject is "create" then
		-- Some Other code here --

		 (* Turn the date given into a applescript 'date' string *) 
		 set myDate to contents of text field "thedate" of tab view item "tabnew" of tab view "maintab" as string
		set theDate to (date myDate)
		set theWeekday to weekday of theDate --result: Saturday
		set theDay to day of theDate --result: 27
		set theMonth to month of theDate --result: February
		 (* Call the subroutine that gets the numeric month *)
		set theNumericMonth to (numericMonth(theMonth))
		 (* See if the day needs to have a zero added to it's front *)
		if ((count characters of (theDay as string)) = 1) then
			set theDay to (("0" & theDay) as string)
		end if

		(* Write the dynamic string *)
		set myselection to ("Ad_Server:*ADS:" & theNumericMonth & " " & theMonth & " 04:" & theDay & " " & theMonth & " " & theWeekday) as string

		-- Some Other code here --
end clicked

on numericMonth(theMonth)
	repeat with i in monthsAsNums
		if ((theMonth of i) as string) is equal to (theMonth as string) then
			set theNumericMonth to theNum of i
		end if
	end repeat
	return theNumericMonth
end numericMonth

You could duplicate the “numericMonth()” subroutine to handle translating your other custom date element formats…for example the "June → “JUN” or "Tuesday → “TUE” translations. Just create a new property in your app with a list of lists contining your key/value pair as above, and a new subroutine which tries to identify the value by it’s key. Make sure to add in some error handling. :wink:


my mind is now blown. i’ve still got a LOT of learning to do it seems.

:stuck_out_tongue: Me too! 8)

Actually, all that date code can just be:


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

wow. this is going to make my script so much more efficient. here’s another question. is there a way, without using a text editor, to take the contents of a text field and make them uppercase?

That’s what I did in that script. Here’s a way to make a subroutine out of it:


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

Well, others have been incredible with the coding part, but I wondered if I might make some suggestions or critique your interface a bit. In all of this, my intention is to be helpful and offer suggestions, and to hopefully encourage an open discussion about interface design issues, not to try to tell you that what you did or the way you did it was wrong. I’m still learning this design kind of stuff myself.

First of all, that’s cool that you can include or reference images right in your post. Most forums I’ve been to disable or discourage that. Anyway…

The title of the “Auto Create” button confused me at first. It wasn’t completely obvious to me what that would mean, but then I realized something. To me, the “Auto” part of “Auto Create” seems a bit redundant, or unnecessary. I mean, it’s a given that your application is helping to automate stuff, by its very nature. There’s an example that the Apple Aqua Human Interface Guidelines (AAHIG) uses that I think is very similar to this situation. It talks about avoiding the inclusion of things that are implied. For example, avoid having a button that says “Save Now” rather than just “Save”. The fact that you’re doing it ‘now’ is implied and shouldn’t be necessary to include it in the title of the button. I think the same might be said in your particular case.

For example, I’d think that “Create Folders” or “Create Job” might be a better-fitting title for that button.

I might also title the tab “New Job” rather than just “New”, which makes it clearer to me. I’m wondering what the other tabs are and what their relationship is to the “New” tab? For example, do they contain settings or something that would apply to the newly created job? If so, I’d consider moving the action button that begins the creation of the job outside of the entire tab view.

I dunno, that’s always been a pet peeve of mine. What I mean is, applications that have settings and stuff in a bunch of different tabs, and then I have to click back on the first tab to get the button to start the process. Something about that just doesn’t seem right to me. One program I can think of that’s like this is ffmpeg. I’m not sure what I would suggest as an alternative as I haven’t thought that one through completely.

In your case though, given the size of your window, if those settings are optional-type settings that would apply to the job you’re creating, then you might just consider using a single window. You could help clarify the fact that they’re optional by disabling them unless a checkbox is first checked. It might be easier to explain that once I know what they apply to.

Then there’s the “Reset” button. First, it looks to be small size, where the rest of the items, like text fields and buttons, are regular size. The AAHIG advises to avoid controls of different size in the same interface and I’d probably have to agree with them here, I think. I’d consider making it full size, or possibly even eliminating it all together. In any case, I think it might be better to place it up closer to the “form” area (with the 3 text fields) that it applies to. You might just make it a flat button and use your own graphic to give it a shape. For example, the “Snap Back” orange arrow in Safari suggests a reset type of gesture. I might dig inside of Safari, hunt that graphic down, bring it into Photoshop, Desaturate it to get rid of the orange color, then bring it to a light or middle gray color and save it as a PNG. Then add it to your project in Xcode, and drag it to the button in Interface Builder.

Then there’s the text fields themselves. While I admit, I do kind of like what’s going on “visually” or aesthetically with the interface, I don’t know if it works in a “practical” sense.

In trying to think of a reasoning behind your choice to use the rounded text fields, I wondered if you may have saw how they seemed to echo the rounded sides of the “Auto Create” button? Maybe that’s reading into it too far, I dunno.

In general though, it’s best to use graphical items in a way that’s consistent with what their “agreed-upon” meaning is. Rounded text fields usually signify a search field. I would probably consider switching to regular rectangle text fields, but that’s up to you.

In any case, I’m wondering if you could save yourself a lot of time in the long run, and even make your app more pleasant to use for the user, by adding a couple of formatters to the fields.

For example, for the Job# field, is there a guaranteed format that the user should enter? Like, the graphics place I had a summer internship at used a 4 digit number for jobs. In that case, you could drag an NSNumberFormatter to that text field in Interface Builder, and then define the types of numbers you’ll accept. At Spartan, when they reached 9999, they started over at 0000, which is something that you could set in the properties in IB. This way, you save yourself from having to do any checks to make sure that what they entered is in the correct format. The same thing might apply to dates, which I’ll get to in a sec.

You might also consider keeping track of what the current job number is, or what the previous number was, and automatically default to setting the contents of the field to the new number. Even if it is little, this could end up saving the end user the hassle and time of having to enter it themselves. And, after all, that’s what your application is all about, isn’t it? Little things like that can add up.

Then, for the “Run Date” field, you might consider adding an NSDateFormatter to the text field and then define, in the Formatter section of IB, the format you’d like the date information to be entered in. Again, here you might consider setting the initial value to the current date.

To do that, you could do something like:

set current_date_ to current date
tell window “Sherpa v1.0.5”
set contents of text field “thedate” of tab view item “tabnew” of tab view “maintab” to current_date_
end tell

I’m pretty sure that the date “object” to which that variable is set will get squished into whatever format you set for the date formatter in Interface Builder.

You might also consider having an NSStepper next to the date field and allow setting of the date that way, similar to how you’d do it in System Preferences.

Then for the Customer field, is that a number too, or a name like “BILMAR” or “SARAH LEE”?

If it’s a name, you might consider using an NSComboBox in the place of a text field. As the name might imply, this combo box is a combination of a text entry field with a drop-down list. This would potentially allow a user to select an existing customer without having to type in the exact name. Also and most importantly I’d think, is that you’ll provide them with a consistent way of referring to the same company, and avoid having 4 different versions of the company’s name, for instance.

Anyway, hope this helps…

thank you for alll of the valuable help! i returned from an extended weekend to find the above post on interfacing that was very helpful. i reworked my interface a bit per the suggestions above:

now here’s my next dilemma. i would like to open a quark 4 template based on the size entered in the “size” text fields. i can make a new document in quark 4 but can’t seem to figure out how to open a template. when i tell quark to open file w/ path to the template, i get a “can’t find file” error. i’m positive the path to the template is correct. what gives?

Oh and here’s the other thing. I can’t for the life of me figure out how to save a quark document with a specific name and then save it to a certain location before closing.