【问题标题】:applescript help naming .plist and removing extentionapplescript 帮助命名 .list 和删除扩展名
【发布时间】:2015-03-12 13:09:49
【问题描述】:

我正在创建一个苹果脚本来创建一个包含属性列表元素但没有 .plist 扩展名的文件!

我的问题是如果我使用对话框来获取文件的名称,例如。

tell application "SystemUIServer"
display dialog "Enter filename :- " buttons {"Generate file"} default answer "Generate Keyfile"
set fileName to text returned of result

然后像这样在桌面上创建文件

set text_file to (path to desktop)'s POSIX path & "" & quoted form of fileName & ".NEWextention"

最后我将元素添加到 .plist

    tell application "System Events"
    tell (make new property list file with properties {name:text_file})
        make new property list item at end with properties {kind:string, name:"regName", value:"FOO"}
        make new property list item at end with properties {kind:string, name:"regNumber", value:"BAR" as text}
    end tell
end tell
end tell

但是,创建的文件在从 fileName 引用时具有 '' 并且仍然具有 .plist 扩展名。
例如输入:- myfile

output:- 'myfile'.NEWextention.plist

当我想要的时候

myfile.NEWextention

如何在 applescript 中实现这一点?

任何帮助将不胜感激! 非常感谢。

              below is the fixed code thanks to @McUsr

如果没有他的帮助,我会在 6 个多月的反复试验中处于同一点

tell application "SystemUIServer"
display dialog "Enter FileName :- " buttons {"Generate file"} default answer "Generate file"
set FileName to text returned of result


set text_file to (path to desktop folder as text) & FileName & ".plist"
set dateStamp to do shell script "date"

tell application "System Events"
    tell (make new property list file with properties {name:text_file})
        make new property list item at end with properties {kind:string, name:"Name", value:FileName}
        make new property list item at end with properties {kind:string, name:"Date", value:dateStamp}
    end tell

end tell

说完

告诉应用程序“Finder” 将文件 text_file 的扩展名设置为“newExt” 结束告诉

【问题讨论】:

    标签: applescript osx-yosemite info.plist


    【解决方案1】:

    您可以在完成处理后通过 Finder (set name extension of file "Hfs:path:to:file:with:name.ext" to "new-ext") 使用系统事件来实现它。

    但是我不确定您是否可以期望系统事件将文件视为属性列表文件,然后包含属性列表项。尽管如此,它仍然值得一试。 :)

    这是您必须更改名称扩展名的方式,因为这不适用于系统事件(名称扩展名是一个只读属性)。

    tell application "Finder"
        set mf to (path to desktop folder as text) & "labels.txt"
        set name extension of file mf to "text"
    end tell
    

    使用文件的 HFS 路径,即 Macintosh Hd:Users:You:path:file.ext,而不是 posix 路径。并且不要使用quoted form of det 路径。

    【讨论】:

    • 感谢您的回答,我现在将尝试实施。我实际上也希望它停止将其视为 .plist 并开始将其视为具有新自定义扩展名的文本文件。所以如果这行得通,那就完美了。
    • 我的意思是tell application "Finder" set name extension of file "~:Desktop:" & quoted form of fileName & ".plist" to ".newext" end tell
    • 波浪号不适用于 hfsPaths,但它可能适用于系统事件。所以试试(path to home folder as text ) & "Desktop:" & fileName(path to desktop folder as text ) & fileName。祝你好运。 :)
    • 不幸的是,无论哪种方式,我都会遇到权限问题:/有没有办法以管理员身份运行它,因为当我尝试它时,由于某种原因它不会让我使用此功能这里是日志Can’t set «class extn» of file (path to «class desk» as text) & quoted form of fileName & ".plist" to ".newEXT". Access not allowed
    • 这是我添加到底部的代码,但它不会编译:( tell application "System Events" set name extension of file (path to desktop folder as text ) & quoted form of fileName & ".plist" to ".newEXT" end tell
    猜你喜欢
    • 2012-01-12
    • 1970-01-01
    • 2019-06-23
    • 2021-11-08
    • 1970-01-01
    • 2021-10-21
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多