Josh
Josh

Reputation: 35

Issue setting returned text to variable within repeat loop - OS X 10.9.x

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
[email protected]" 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

Upvotes: 3

Views: 344

Answers (1)

jweaks
jweaks

Reputation: 3792

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

Upvotes: 1

Related Questions