我完全忘记了Xcode AppleScript应用程序。
基本上,应用程序将从plist文件中读取值。
我读取这个变量的应用程序描述是
set the plistfile_path to "~/Desktop/example.plist"
tell application "System Events"
set p_list to property list file (plistfile_path)
-- read the plist data
set theNameFromPlist to value of property list item "theName" of p_list
set theEmailFromPlist to value of property list item "theEmail" of p_list
set thecreationDateFromPlist to value of property list item "creationDate" of p_list
end tell
现在我该如何将其集成到Xcode?
我应该在AppDelegate.applescript上,在"applicationWillFinishLaunching_(aNotification)"之后的任何位置添加该脚本吗?
followed by property NametextField : theNameFromPlist
我可能完全错了,只是无法思考。PS swift会做任何更容易的
就像vadian在评论中所说的,在Xcode中,系统事件无法正常运行,所以你可以只使用原生的Cocoa类,但我更喜欢使用"doshell脚本"appscript/asobjc命令并运行"defaults"命令,因此当应用程序完成启动时,有一种方法可以做到这一点:
on applicationWillFinishLaunching_(aNotification)
set plistFile to "~/Desktop/example.plist"
set theNameFromPlist to do shell script "defaults read " & plistFile & " theName"
set theEmailFromPlist to do shell script "defaults read " & plistFile & " theEmail"
set theCreationDateFromPlist to do shell script "defaults read " & plistFile & " creationDate"
end applicationWillFinishLaunching_
如果您试图在窗口的TextField中显示名称在界面生成器中,您只需添加修改代码即可:
property nameTextField : missing value
on applicationWillFinishLaunching_(aNotification)
set plistFile to "~/Desktop/example.plist"
set theNameFromPlist to do shell script "defaults read " & plistFile & " theName"
set theEmailFromPlist to do shell script "defaults read " & plistFile & " theEmail"
set theCreationDateFromPlist to do shell script "defaults read " & plistFile & " creationDate"
tell nameTextField to setStringValue_(theNameFromPlist)
end applicationWillFinishLaunching_
(确保将属性名称TextField连接到接口生成器中的TextField)
希望这有帮助!!:D
提到Cocoa类,我实际上是指这个
set plistFile to POSIX path of (path to desktop) & "example.plist"
set thePlist to (current application's NSDictionary's dictionaryWithContentsOfFile:plistFile) as record
set theNameFromPlist to thePlist's theName
set theEmailFromPlist to thePlist's theEmail
要添加新的键值对并将plist写回磁盘,请添加
set thePlist to thePlist & {stringKey:"Foo", booleanKey:false}
set cocoaDictionary to current application's NSDictionary's dictionaryWithDictionary:thePlist
cocoaDictionary's writeToFile:plistFile atomically:true