Applescript code help

Discussion in 'Mac Programming' started by dMXyEYe, Feb 7, 2014.

  1. dMXyEYe macrumors newbie

    Joined:
    Jul 8, 2013
    Location:
    Poland
    #1
    Hi there!

    I've got a problem.

    What do I have to write in applescript to make something like this happen:

    Code:
    tell application "System Events" to key code 123 or 124 or 125 or 126 using control down. 
    I mean I want to system events to chose a key code from those three codes and use it with control down.

    Is it possible to make? And It would be good to make it random..Once use 123 once 126 and what is IMPORTANT: do not use key code twice.

    Example:
    Code:
    Use 123, then use 126 or 125 or 124
    
    Use 124, then use 126 or 125 or 123
    
    hm?
     
  2. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #2
    Applescript's Standard Additions has a random number command which you could use like this.

    Code:
    set myRandomKeyCode to random number from 123 to 126
    tell application "System Events" to key code myRandomKeyCode using control down
    
    Or alternativly you could put your key codes into an Applescript list, and then randomly retrieve on of the values from the list, like this.

    Code:
    set randomKeyCodeList to {123, 124, 125, 126} as list
    set randomNumber to random number from 1 to count of randomKeyCodeList
    tell application "System Events" to key code (item randomNumber of randomKeyCodeList) as integer using control down
    
    There are numerous other ways to use Applescript's random number command, so I would suggest reading the Standard Additions scripting definition dictionary, for a more definative guide to the random number command.

    Hope this is of some help.

    Regards Mark
     
  3. dMXyEYe thread starter macrumors newbie

    Joined:
    Jul 8, 2013
    Location:
    Poland
    #3
    Ok, great, but what will happen if it will chose twice the same number?

    Example:

    123 and 123 ?


    It must not chose the same number twice. How to write some kind of command to avoid this?
     
  4. numlock macrumors 68000

    Joined:
    Mar 13, 2006
    #4
    can i ask what the purpose is?

    since you have so few numbers and therefore limited randomness why not just create your own sequence and have applescript follow that?

    but the randomnumber in applescript is brilliant. a friend had an ad on a messaging board and it was always bumped on the same schedule so i believe he got cited so i wrote a script with totally random intervals
     
  5. dMXyEYe thread starter macrumors newbie

    Joined:
    Jul 8, 2013
    Location:
    Poland
    #5
    I know but I need it for like anti-afk script.

    So I can move like north, then east, then somewhere else, but I can't be north twice.

    Understand?

    And why it must be a random? Because it must act like a normal human. I think nobody will move

    North, then east, then nort, then east for like 12h. ;<
     
  6. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #6
    You can just randomize the movement list, for example:
    Code:
    return shuffle({123, 124, 125, 126}) # example
    
    on shuffle(someList) # randomize contents of a list
    	set shuffledList to {}
    	repeat # forever
    		set someitem to some item of someList # get a random list item
    		if someitem is not in shuffledList then set end of shuffledList to someitem
    		if (count shuffledList) = (count someList) then exit repeat # shuffled
    	end repeat
    	return shuffledList
    end shuffle
     
  7. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #7
    Red Menace's idea is a good solution, and you could also do something like this.

    Code:
    property lastRandomKeyCode : missing value
    
    set randomKeyCode to my PickRandomKeyCode()
    tell application "System Events" to key code randomKeyCode using control down
    
    on PickRandomKeyCode()
    	repeat
    		set randomKeyCode to random number from 123 to 126
    		if randomKeyCode ≠ lastRandomKeyCode then
    			set lastRandomKeyCode to randomKeyCode
    			exit repeat
    		end if
    	end repeat
    	return randomKeyCode
    end PickRandomKeyCode
    
    The above code would not allow the same random number to be repeated consecutively, and you could also do something similar if you didn't want to ever repeat a random number more than once.

    Regards Mark
     
  8. Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #8
    Just to clarify if you havn't seen the "≠" character before, it's the not equal to symbol in Applescript, so you can write this line from the above example.

    Code:
    if randomKeyCode ≠ lastRandomKeyCode then
    
    Like this.

    Code:
    if randomKeyCode is not equal to lastRandomKeyCode then
    
    Regards Mark
     

Share This Page