variables - 问题设置返回文本到重复循环中的变量 - OS X 10.9.x

标签 variables loops applescript handlers

这个脚本的目标是:

  1. 询问用户他们想要多少个文本替换(系统偏好设置>键盘>文本)快捷方式。返回的文本设置为我的变量“gTextReplacementNum”作为数字。 查看我的第二个处理程序 “HowMany()”

  2. 让用户提供文本替换快捷方式和文本以根据他们想要的快捷方式数量进行替换。 查看我的第三个处理程序 “GetText()”

  3. 将用户提供的文本包含在一个变量中,创建一个新的 AppleScript 文档,为他们完成所有繁重的工作。代码尚未编写;不在提问范围内。

  4. 然后他们有一个个性化的 AppleScript 应用程序包,他们可以在他们的 Mac 上启动以自动填充文本替换首选项面板。

我无法让它正常工作。我需要循环不断将答案添加到作为列表的变量或添加到根据循环实例递增其名称的变量(例如 TextReturned_i、TextReturned_i+1 等)。

我是否充分解释了这一点?

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

最佳答案

在您的 GetText() 处理程序中,您每次都替换值 TextShortcut 和 TextReplaced。你需要

set aList to aList & newValue

在重复循环中构建列表。

此外,按原样,此处理程序从不返回这两个列表的值。因此,我建议使用您的方案,将这两个变量也设为全局变量。 所以,完整的变化是: 1. 添加到声明中:

global gTextReplacementNum
global gTextShortcut
global gTextReplaced

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

和 2. 编辑您的 GetText() 处理程序:

-- 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

另一种方法是读取制表符分隔 rune 件并使用标准脚本从中进行处理。像这样的东西:

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

关于variables - 问题设置返回文本到重复循环中的变量 - OS X 10.9.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23964055/

相关文章:

c++ - 循环和if的优化

windows - AppleScript 获取窗口的标题

macos - Spotify + AppleScript : Add current track to playlist

MySql 查询中的 PHP 变量

html - 每周批处理日?

python - Python 3 中变量的简单表达式

c - 静态变量和全局变量的使用

c - 在 openmp 中嵌套 for 循环

python - 循环访问一组具有相似名称的变量

macos - 学习 AppleScript