Simple old vs new vaiable -help

Hi, I’m certainly a newbie… could someone please help me with this,
seems rather simple in php/etc but scpt’s are new to me.

Question: how to repeat a script comparing old variable against a new one:


on idle
			-- OLD VS NEW CHECK
			if bc_number is not bc_number_old then
				-- TICKET TYPE CHECK
				if bc_number starts with "1" then
					set ok_flag to "Y"
				else if bc_number starts with "2" then
					set ok_flag to "Y"
				end if
			end if

-- more stuf, then

set bc_number_old to bc_number
10 --seconds to wait
end idle

The issue is that I havent declaired “bc_number_old” until after. How do I set
it to “void” initially, then have it become the current value of “bc_number”
Thanks in advance!

You can use a try/end try with error catching block:

set bc_number to 4444

try
	bc_number_old
on error --> undefined!
	set bc_number_old to bc_number
end try

--> whatever, now both are defined variables
property bc_number_old : missing value

Is this the droid you’re looking for?

Wow, thanks guys looks like that will work… simple for sure.

Could I have you look at the entire sctp? I’m certain there is a much more compact
way of writing this… again newbie here in applescript, but fully versed in php/mysql/etc

Thanks!

Fire away. Lots of experienced scripters 'round these parts.

If the script is more than 50 lines or so, it might be better to link to a copy of it in plain text form somewhere.

Thanks Mikey-San, I must say that I am super impressed with this community --top notch!

Concept:
This is a monitor .app to detect Safari and print a barcode from a Dymo Lable printer
based on the variable. It is going to be used at the check-in for an event on Friday
here in Detroit (www.createdetroit.com/dge)

  1. Only way I could figure out how to pass a var from Safari to scpt was the “window name”
  2. The barcode is 13 digits long
  3. Two types are going to be passed, one starting with “1” and the other with “2” --e.g. 1000000000068
  4. Since it monitors every couple sec., it should not repeat (print) after detecting the same barcode var

I’m certain there is a much better (faster) way to do this but, again, I’m new to sctp (my first, in fact)


global bc_number_old
on idle

tell application "Safari"
	activate
	set target_URL to "http://conference.local"
	set current_URL to URL of document 1
	
	-- GET CURRENT URL - Make sure event page
	if current_URL contains target_URL then
		set bc_number to name of front window --title of safari window
		set bc_count to count bc_number
		set ok_flag to "N"
		
		if bc_count is 13 then
			
			-- OLD VS NEW CHECK  -- NEED HELP HERE !
			--if bc_number is not bc_number_old then
			
			-- TICKET TYPE CHECK
			if bc_number starts with "1" then
				set ok_flag to "Y"
			else if bc_number starts with "2" then
				set ok_flag to "Y"
			end if
			
			--end if
			
			-- TESTER
			--display dialog bc_number buttons {""} with icon 1 giving up after 2
			-- END TESTER
			
			-- DYMO ROUTINE
			if ok_flag = "Y" then
				try
					tell application "DYMO Label"
						loadPlugin name "Label Editor"
						
						if bc_number starts with "1" then
							-- BEST OF SHOW TICKET
							openLabel in "/Applications/DYMO Label/Label Files/DGE_BestofShow-Shipping (30256).DLF"
						end if
						
						if bc_number starts with "2" then
							-- AUCTION TICKET
							openLabel in "/Applications/DYMO Label/Label Files/DGE_Auction-Shipping (30256).DLF"
						end if
						
						tell current plugin
							set obj to make new barcode at end
							tell obj
								set xPosition to 70
								set yPosition to 100
								set width to 200
								set barcodeSize to small
								set barcodeText to bc_number
								set barcodeType to Code39
								set fontName to "Arial"
								set fontSize to "12"
								set hrPosition to bottom
							end tell
						end tell
						-- PRINT DYMO
						--printLabel2
						redrawLabel
					end tell
				on error error_message
					display dialog error_message buttons {"Cancel"} default button 1
				end try
				
			end if -- END OK FLAG
			-- SET OLD BC_NUMBER FOR NEXT CHECK
			set bc_number_old to bc_number
			
		end if --END COUNT
	end if
end tell

2 (* number of seconds to wait *)
end idle

These are just some random notes I jotted down about your script; ponderances on which you may wish to meditate.

The difference between setting a variable (set x to y) and declaring it as a property (property x : “value”) is that the former is done at runtime, during script execution, and the latter sets variables at launch. For some scripts, this is important as a matter of executing properly; for others, this is strictly a matter of efficiency.

For your script, the following variables never change through runs:

target_URL, xPosition, yPosition, width, barcodeSize, barcodeType, fontName, fontSize, hrPosition

The way your script runs currently, each time it takes a pass through, those variables are all set again–to the same values. You might consider declaring them at launch in properties instead: you’ll add a virtually undetectable amount of time to launch, but you won’t be needlessly resetting these variables each time the script body executes.

I’m confused by the ticket type check. If you’re only getting two ticket strings, one beginning with “1” and one beginning with “2”, what exactly does this do:

if bc_number starts with "1" then
   set ok_flag to "Y"
else if bc_number starts with "2" then
   set ok_flag to "Y"
end if

Unless I’m missing something, you look to see if the string begins with “1”. If it does, you set ok_flag to “Y”; if it doesn’t, you set ok_flag to . . . “Y”. Oversight? Should this set a different value for ok_flag on “2”?

It looks like it might be control of some kind, in the event the page title is not in the proper string form. In that case, you could just do this:

if bc_number starts with "1" or bc_number starts with "2" then
   set ok_flag to "Y"
end if

This is very important to note:

Your main control is checking to see if bc_count is 13 characters long, to make sure you’ve got a page with a bar code on it. Here’s another frequently seen window title that also is 13 characters long:

“404 Not Found”

You need a new validation method here. You could check title length and if it begins with “1” or “2”, but that’s still kinda kludgy. You might consider making your successfully generated page titles something like this:

Barcode: 1234567890123

Then replace this code:

set bc_number to name of front window --title of safari window
set bc_count to count bc_number
set ok_flag to "N"

if bc_count is 13 then

With this code:

set page_title to name of front window --title of safari window
set bc_number to (characters 10 through 22 of page_title) as text
set ok_flag to "N"

if page_title begins with "Barcode: " then

I have to go get ready for work, so I must stop here, but I’ll check back later today to see if you have any questions.

Postscript: Aren’t Dymo printers just a BLAST? :wink:

Mickey-San, thank you so very much for taking the time to look over my script so carefully!

Your insight is great… even made me laugh a bit. Great catch on the 404 error, that
was super cool.

Both of your suggestions are spot on. I didn’t know how to do the “or” statement
–keep getting errors, but again it was simpler that I thought.

I’ll certainly test these are report back to you later tonight.
I truly appreciate your time, hope I can do the same for someone
here soon enough.

Take care.

So, I ended up going with a complete workaround --using PHP and osascript --works great!
with the DYMO attached to a G4 I made into an Apache server

--------PHP code-----------
function dymo_print($bc_number, $prnt) {

// DEBUG - COMMENT OUT
	//$bc_number = "9000000000030";

if($bc_number != "") {
	$bc_flag = $bc_number{0};

	// BEGIN DYMO
		exec("osascript -e 'tell app \"DYMO Label\" to loadPlugin name \"Label Editor\"'");

	// SET TEMPLATE
	if($bc_flag == 1) {
		// BEST OF SHOW
			exec("osascript -e 'tell app \"DYMO Label\" to openLabel in \"/Applications/DYMO Label/Label Files/DGE_bos_final.DLF\"'");
	
	} elseif($bc_flag == 2) {
		// AUCTION
			exec("osascript -e 'tell app \"DYMO Label\" to openLabel in \"/Applications/DYMO Label/Label Files/DGE_auc_final3.DLF\"'");
	
	}
	
	// SET BARCODE
			exec("osascript -e 'tell app \"DYMO Label\" to tell current plugin to set (content of (print object named \"bctxt\")) to \"".$bc_number."\"'");
	
			exec("osascript -e 'tell app \"DYMO Label\" to tell current plugin to set (barcodeText of (barcode named \"Barcode\")) to \"".$bc_number."\"'");
	
	
	// PRINT LABEL NOW + UPDATE
		exec("osascript -e 'tell app \"DYMO Label\" to redrawLabel'");
		
		if($prnt == "Y") {
			exec("osascript -e 'tell app \"DYMO Label\" to printLabel2'");
		}
}					

}

-------END -----

Hope this helps someone, everyone here has been so kind… and fast!
Take care, gotta finish up the rest of the scripts.