该脚本的目标是:
询问用户他们想要多少个文本替换(“系统偏好设置”>“键盘”>“文本”)快捷方式。返回的文本被设置为我的变量“ gTextReplacementNum”的数字。参见我的第二个处理程序 “ HowMany()”
让用户提供文本替换快捷方式,并用文本替换所需数量的快捷方式。看到我的第三个处理程序 “ GetText()”
以用户提供的包含在变量中的文本为例,创建一个新的AppleScript文档,以完成所有繁重的工作。代码尚未编写;不在问题范围内。
然后,他们可以在Mac上启动个性化的AppleScript应用程序捆绑包,以自动填充文本替换首选项窗格。
我很难让它正常工作。我需要循环以不断将答案添加到列表中的变量或根据循环实例(例如,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
[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
在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
另一种方法是读取制表符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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句