3

The goal of this script is to:

  1. Ask the user how many text replacement (System Preferences>Keyboard>Text) shortcuts they'd like to have. The text returned is set to my variable "gTextReplacementNum" as number. See my second handler "HowMany()"

  2. Have the user provide the text replacement shortcut and the text to do the replacing for the number of shortcuts they wanted. See my third handler "GetText()"

  3. Take the user provided text contained in a variable to create a new AppleScript doc that does all the heavy lifting for them. Code not yet written; not within scope of question.

  4. Then they have a personalized AppleScript Application Bundle they may launch on their Mac to auto-populate the text replacement preferences pane.

I am having trouble getting this to work properly. I need the loop to keep adding the answers to a variable as a list or to a variable that increments its name according to the loop instance (e.g. TextReturned_i, TextReturned_i+1, etc).

Have I adequately explain this?

global gTextReplacementNum
set gTextReplacementNum to 0

# Main Logic Begins

try

    Start()

    HowMany()

    GetText()

on error errText number errNum
    display alert "Error " & errNum message errText

end try

# Main Logic Ends



# Handlers Begin

-- First Handler
on Start()
    display alert "Automated Text Replacement v1.0" message "Created by: Me
myemail@domain.com" buttons {} giving up after 4
    display alert "About" message "This app will have you provide a text 'short cut' to replace with and replacement text. It then compiles all this into an application that can be run on any Mac.

    Would you like to continue?" buttons {"No", "Yes"} cancel button 1 default button 2
end Start


-- Second Handler
on HowMany()
    display dialog "How many text replacement shortcuts would you like?

    Please enter numericals only. (1, 2, 3)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
    copy the result as list to {ButtonPressed, TextReturned}
    set gTextReplacementNum to TextReturned as number
end HowMany

-- Third Handler
on GetText()
    repeat with i from 1 to gTextReplacementNum as number
        display dialog "What text would you like to replace?
        (this is your shortcut)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
        set TextShortcut to text returned of result as list

        display dialog "What is the replacement text?
        (this is what the shortcut fills out)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
        set TextReplaced to text returned of result as list
    end repeat
end GetText

# Handlers End
Josh
  • 35
  • 4

1 Answers1

1

In your GetText() handler, you are replacing the value TextShortcut and TextReplaced each time. You need to

set aList to aList & newValue

to build a list in a repeat loop.

Also, as is, this handler never returns the value of these two lists. So, I'd suggest, using your scheme, make these two variables globals as well. So, the full changes are: 1. Add to the declarations:

global gTextReplacementNum
global gTextShortcut
global gTextReplaced

set gTextReplacementNum to 0
set gTextShortcut to {}
set gTextReplaced to {}

and 2. edit your GetText() handler:

-- Third Handler
on GetText()
    repeat with i from 1 to gTextReplacementNum as number
        display dialog "What text would you like to replace?
        (this is your shortcut)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
        set gTextShortcut to gTextShortcut & (text returned of result)

        display dialog "What is the replacement text?
    (this is what the shortcut fills out)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
        set gTextReplaced to gTextReplaced & (text returned of result)
    end repeat
end GetText

An alternate method would be to read a tab delim file and work from that with a standard script. Something like:

property fileName : "shortcuts.txt"

set filePath to (path to desktop as string) & fileName
set theData to read file filePath

set theRecords to paragraphs of theData
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab

repeat with thisPair in theRecords
    set {theShortcut, theReplacement} to text items of thisPair
    setKeyboardPref(theShortcut, theReplacement)
end repeat
set AppleScript's text item delimiters to oldDelim

on setKeyboardPref(theShortcut, theReplacement)
    -- set up the pair
    display dialog theShortcut & return & theReplacement
end setKeyboardPref
jweaks
  • 3,674
  • 1
  • 19
  • 32
  • Thank you very much jweaks, it works perfectly! I had no idea where to begin. You're explanation not only got it working, but also helped me understand you're logic in making it work. Much appreciated. :) – Josh Jun 01 '14 at 21:26
  • Josh, glad it's working. I must say, answering all those dialogs would not be fun for the user. How about having them create a text file with shortcut [tab] replacement. One pair on each line, then your script can take it from there. – jweaks Jun 02 '14 at 01:46
  • Well, that's a fantastic idea. I just duplicated my script to try this approach. My initial thoughts for this idea are to include a .txt template file to lessen user error and make the script use a droplet action of that text file for script activation. I'll update later. Thanks. – Josh Jun 04 '14 at 20:05
  • Sure. Give them a text file, with a fixed name, tell them to place it on the desktop, and then your script can read the file, parse it and do the work. The same script will work for everyone. – jweaks Jun 04 '14 at 20:11
  • I'm quite the novice with AppleScript. It just occurred to me that a 'choose file with prompt' start would be much easier for this. – Josh Jun 04 '14 at 20:29
  • I've added a sample script of this tab delim file to the answer above to get you started on how to implement. – jweaks Jun 04 '14 at 21:35
  • jweaks, please review your alternative method code at your convenience; it appears flawed. Unless, of course, that was intentional. e.g. 'thisPair' 'setKeyboardPref(' – Josh Jun 04 '14 at 22:19
  • It works perfectly. It has no error checking, yet. In other words, you need to put a text file on the desktop, named shortcuts.txt, with proper formatting of a string a tab and a string on each line. You can replace the desktop file code with a choose file if you like. – jweaks Jun 04 '14 at 23:46