Variables 问题设置将文本返回到重复循环中的变量-OS X 10.9.X
此脚本的目标是:Variables 问题设置将文本返回到重复循环中的变量-OS X 10.9.X,variables,loops,applescript,handlers,Variables,Loops,Applescript,Handlers,此脚本的目标是: 询问用户希望有多少个文本替换(系统首选项>键盘>文本)快捷键。返回的文本被设置为我的变量“gTextReplacementNum”作为编号。请参阅我的第二个处理程序“HowMany()” 让用户提供文本替换快捷方式,并根据所需的快捷方式数量替换要替换的文本。请参阅我的第三个处理程序“GetText()” 获取包含在变量中的用户提供的文本,以创建一个新的AppleScript文档,该文档为他们完成所有繁重的工作。尚未编写的代码;不在问题的范围之内 然后,他们可以在Mac上启动个性
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 {}
二,。编辑GetText()处理程序:
另一种方法是读取tab delim文件并使用标准脚本从该文件开始工作。比如:
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,它工作得非常好!我不知道从哪里开始。你的解释不仅使它起作用,而且帮助我理解你使它起作用的逻辑。非常感谢。:)乔希,很高兴它起作用了。我必须说,回答所有这些对话框对用户来说都不是一件有趣的事。让他们用快捷方式[tab]替换创建一个文本文件怎么样。每行一对,然后你的脚本就可以从那里开始了。好吧,这是个好主意。我只是复制了我的脚本来尝试这种方法。我最初的想法是包含一个.txt模板文件,以减少用户错误,并使脚本使用该文本文件的一个水滴动作来激活脚本。我稍后会更新。谢谢,当然可以。给他们一个固定名称的文本文件,告诉他们把它放在桌面上,然后你的脚本就可以读取文件,解析它并完成工作。同样的脚本对每个人都适用。我是AppleScript的新手。我突然想到,“用提示符选择文件”开始会更容易。
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