Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 22, 2013, 10:43 PM   #1
X4OX0
macrumors newbie
 
Join Date: Jun 2013
Greater than or equal to problem.

Hey All,



I'm building a script for Final Cut Pro (7) this is a small part of the entire script and I am having some difficulties getting this to work. So, the objective is to get a number from the user, then to have the number escalate as it repeats. I also need to make this number "Paste" in. However I cannot use the cliboard functions at all because they will be in use in a different part of the script.



What this part was designed to do, (but doesn't do all the time ) is take the number submitted by the user, and break it down by digit and spit it back out in the form of a variable key code. Another part of the script would do the escalation of the number. But what I really need is to find out why the greater than or equal to/ less than or equal to aren't correctly segmentig the numbers, so the script doesn't try to do the input number more than once.





Code:
set start_value to text returned of (display dialog "Start number?: " default answer "") as number

 

if start_value is greater than or equal to 0 then

    if start_value is less than or equal to 9 then

        set start_value to start_value as string

        set start_value01 to 0

        set start_value02 to 0

        set start_value03 to the first item of start_value as number

       

    end if

end if

if start_value is greater than or equal to 10 then

    if start_value is less than or equal to 99 then

       

        set start_value to start_value as string

        set start_value01 to 0

        set start_value02 to the first item of start_value as number

        set start_value03 to the second item of start_value as number

       

    end if

end if

 

if start_value is greater than or equal to 100 then

    if start_value is less than or equal to 999 then

       

        set start_value to start_value as string

        set start_value01 to the first item of start_value as number

        set start_value02 to the second item of start_value as number

        set start_value03 to the third item of start_value as number

       

    end if

end if

 

if start_value01 is 1 then set O1 to 18

if start_value01 is 2 then set O1 to 19

if start_value01 is 3 then set O1 to 20

if start_value01 is 4 then set O1 to 21

if start_value01 is 5 then set O1 to 23

if start_value01 is 6 then set O1 to 22

if start_value01 is 7 then set O1 to 26

if start_value01 is 8 then set O1 to 28

if start_value01 is 9 then set O1 to 25

if start_value01 is 0 then set O1 to 29

 

if start_value02 is 1 then set O2 to 18

if start_value02 is 2 then set O2 to 19

if start_value02 is 3 then set O2 to 20

if start_value02 is 4 then set O2 to 21

if start_value02 is 5 then set O2 to 23

if start_value02 is 6 then set O2 to 22

if start_value02 is 7 then set O2 to 26

if start_value02 is 8 then set O2 to 28

if start_value02 is 9 then set O2 to 25

if start_value02 is 0 then set O2 to 29

 

if start_value03 is 1 then set O3 to 18

if start_value03 is 2 then set O3 to 19

if start_value03 is 3 then set O3 to 20

if start_value03 is 4 then set O3 to 21

if start_value03 is 5 then set O3 to 23

if start_value03 is 6 then set O3 to 22

if start_value03 is 7 then set O3 to 26

if start_value03 is 8 then set O3 to 28

if start_value03 is 9 then set O3 to 25

if start_value03 is 0 then set O3 to 29

 

tell application "TextEdit"

    activate

    tell application "System Events"

        tell process "TextEdit"

            key code O1

            delay 0.1

            key code O2

            delay 0.1

            key code O3

        end tell

    end tell

end tell




Text Edit being my test dummy for this part of the operation. I am on 10.8, a couple others will be on 10.7.



Thanks for any help on this!



Gabe

Last edited by dejo; Jun 22, 2013 at 11:46 PM. Reason: please use [code] tags.
X4OX0 is offline   0 Reply With Quote
Old Jun 22, 2013, 11:35 PM   #2
HexMonkey
Administrator
 
HexMonkey's Avatar
 
Join Date: Feb 2004
Location: New Zealand
The problem is that if you have a line such as:
if start_value is greater than or equal to 100 then
where start_value is a string, then AppleScript will perform a text comparison - ie is start_value after, alphabetically, "100". This is true for values such as "5" or "43".

To solve this, just convert start_value to a number first, eg:
if (start_value as number) is greater than or equal to 100 then
Alternatively, change your code so that it doesn't convert start_value to a string, or use 'else if' statements rather than 'if' so that it doesn't check for multiple cases where only one should be possible anyway.

However, your code can be greatly simplified, which avoids the problem altogether. The following is functionally equivalent to your original code (with a little extra code for error checking):

Code:
set start_value to text returned of (display dialog "Start number?: " default answer "")

try
	set start_value to start_value as integer
	if (start_value < 0 or start_value > 999) then
		-- not in the required range - do something
	else
		set start_value03 to start_value mod 10
		set start_value02 to start_value mod 100 div 10
		set start_value01 to start_value mod 1000 div 100
		
		tell application "TextEdit"
			activate
			tell application "System Events"
				
				tell process "TextEdit"
					keystroke (start_value01 as string)
					delay 0.1
					keystroke (start_value02 as string)
					delay 0.1
					keystroke (start_value03 as string)
				end tell
			end tell
		end tell
		
		-- do whatever else you want to do
	end if
on error e
	-- not a number (or some other error) - do something
end try
HexMonkey is offline   1 Reply With Quote
Old Jun 25, 2013, 11:55 AM   #3
X4OX0
Thread Starter
macrumors newbie
 
Join Date: Jun 2013
That works flawlessly, thank you, thank you, thank you!!!!
X4OX0 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:44 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC