0

I want to write a script in AppleScript that will output all numbers from 0000 to 9999 in turn to an application (here it is System Preferences) using the keystroke command. I have this so far that will do one number:

tell application "System Preferences" to activate
delay 0.5
tell application "System Events"
    keystroke (1234)
end tell

But I would have to manually make 9998 more of these, which is obviously undoable.

I also have text file that has all of the numbers from 0000 to 9999 in a format like:

0000 0001 0002 0003 0004 0005

etc, and also a text file formatted like

0001
0002
0003
0004
0005

etc. How could I import each of these numbers in turn to the keystroke command in the script I wrote above, so that it would go through all of those numbers in the same script? Maybe in a repeating script?

I would also want to output a terminal command after each keystroke command, but I think I could manage that.

Any help would be appreciated!

oguz ismail
  • 1
  • 16
  • 47
  • 69

2 Answers2

0

You have several questions there, but they all involve using the repeat statement:

tell application "Whatever" to activate -- this is where the keystrokes will go
delay 0.5

# repeat from startingValue to endingValue
set prefix to "0000" -- for leading zeros
repeat with aValue from 0 to 1000
   tell application "System Events"
      keystroke text -4 thru -1 of (prefix & aValue)
   end tell
end repeat

# repeat with words in some text (punctuation, etc are skipped)
set test to read "/path/to/words/file.txt" -- POSIX path
repeat with aWord in (words of test)
   tell application "System Events"
      keystroke aWord
   end tell
end repeat

# repeat with paragraphs in some text (handles different line endings)
set test to read "disk:path:to:paragraphs:file.txt" -- HFS path
repeat with aParagraph in (paragraphs of test)
   if aParagraph is not in {"", return} then -- no empty paragraphs
      tell application "System Events"
         keystroke aParagraph
      end tell
   end if
end repeat

See the AppleScript Language Guide for language features and syntax.

red_menace
  • 3,162
  • 2
  • 10
  • 18
0

Assuming you saved following valid Terminal commands as plain text, in the .txt file, and Terminal prompt is already active,

echo 'Hello, one'
echo 'Hello, two'
echo 'Hello, three'

you can execute commands number 2 and 3 using following simple script:

set {startNumber, endNumber} to {2, 3}

set texfFile to choose file of type "public.text"
set theParagraphs to read texfFile

tell application "Terminal" to activate -- required
tell application "System Events"
    repeat with i from startNumber to endNumber
        keystroke (paragraph i of theParagraphs) & return
    end repeat
end tell

Generally, it is better to not use GUI scripting at all:

set {startNumber, endNumber} to {2, 3}

set texfFile to choose file of type "public.text"
set theParagraphs to read texfFile

tell application "Terminal"
    activate
    repeat with aNumber from startNumber to endNumber
        do script (paragraph aNumber of theParagraphs) in window 1
    end repeat
end tell
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8