此脚本的目标是:
询问用户他们希望有多少文本替换(系统偏好设置>键盘>文字)快捷方式。返回的文本设置为我的变量" gTextReplacementNum"作为数字。 查看我的第二个处理程序 " HowMany()"
让用户提供文本替换快捷方式和文本,以替换他们想要的快捷方式数量。 查看我的第三个处理程序 " GetText()"
获取变量中包含的用户提供的文本,以创建一个新的AppleScript文档,为他们完成所有繁重的工作。代码尚未写入;不在问题范围内。
然后他们有一个个性化的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
答案 0 :(得分:1)
在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
另一种方法是读取制表符分隔文件,并使用标准脚本从中进行操作。类似的东西:
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