2016-08-03 69 views
0

我完全忘记了Xcode AppleScript应用程序。Xcode:读取plist数据并将它们显示为文本

基本上,应用程序将从plist文件读取值。

我的AppleScript来读取这个变量是

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迅速将做到这一点,如果任何容易

+0

在Xcode中忘了'系统Events'和使用AppleScriptObjC中的原生Cocoa类。 – vadian

回答

1

就像vadian说,在评论,在Xcode中,系统事件将无法正常运行,所以你可以只使用天然可可类,但我更喜欢使用 “做shell脚本” AppleScript的/ asobjc命令并运行了“默认值”命令,所以这里是一个办法做到这一点,当应用程序将完成启动:

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_ 

而且,如果你想在一个文本字段,以显示在一个窗口 名称您可以添加Interface Builder来修改代码:

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_ 

(确保财产nameTextField连接到TextField在界面生成器)

希望这有助于! :d

+0

这看起来不错,我可以试一下,谢谢 –

+0

顺便说一句,对于更复杂的\ sllas目录例如../Applications/My \ Awesome \ App.app /然后我建议修改代码。 do shell script“defaults read”&plistFile&“theName”,use:do shell script“defaults read \”“&plistFile&”\“theName”(我之前没有添加\“因为当使用〜/ in”\ ,它不能正常工作,但很高兴我可以帮助) – Lucasware

1

提起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 
+0

这是非常有用的!我如何去写(即修改)plist中的一个项目?上述命令似乎只适用于阅读。我有一个字符串和布尔值,我想写入文件。你可能会添加那部分数据被保存回plist的部分吗?这会帮助我很多。 – ProGrammer

+0

我更新了答案。 – vadian

+0

我很困惑,因为我正在寻找采取文本字段的值和复选框的状态并在plist中更新它们。最初,我使用顶级代码从plist读取数据并设置组件的文本/状态。现在,我添加了一个保存按钮来写回数据而不用替换整个文件(只需更新值)。有些内容符合:'将Plist的savedText设置为guiTextbox的stringValue()'。是否有一种简单的方法来更新单个值,用一些简单的方法呢? – ProGrammer

相关问题