如何从 Omnigraffle 获取没有扩展名的文件名? [英] How to get filename without extension from Omnigraffle?

查看:31
本文介绍了如何从 Omnigraffle 获取没有扩展名的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Omnigraffle Professional 5 中获取没有当前文档文件扩展名的文件名.

I'm trying to get the filename without the extension of the current document file in Omnigraffle Professional 5.

tell application "OmniGraffle Professional 5"
    set _document to front document
    set _path to path of _document

    -- Get filename without extension
    tell application "Finder"
        set {_filename, _extension, _ishidden} to the
             {displayed_name, name_extension, extension_hidden} of the _path
    end tell
end tell

这给了我以下错误:error "Can't get display_name of \"/Users/ca/Downloads/Feb 8.graffle\"."数字 -1728 来自/Users/ca/Downloads/Feb 8.graffle"的显示名称.

我找到了一些相关的问题和页面,但我有点迷茫,真的不明白为什么它不起作用.

I found some related questions and pages, but I'm a bit lost and really can't understand why it does not work.

感谢您的帮助!

推荐答案

您需要将其更改为以下内容:

You'd need to change it to the following:

tell application "OmniGraffle Professional 5"
    set _document to front document
    set _path to path of _document

    -- Get filename without extension
    tell application "Finder"
    set {_filename, _extension, _ishidden} to the ¬
               {displayed name, name extension, extension hidden} ¬
                of ((the _path as POSIX file) as alias)
    end tell
    if (_extension ≠ missing value) then
        set baseName to text 1 thru -((length of _extension) + 2) of _filename
    end if

end tell

"path of front document" 返回文件的 POSIX 路径,它只是一个普通的字符串.为了能够获取有关项目的信息,Finder 需要对该文件的别名引用.当您传递纯字符串时,它会出错,因为纯字符串不具有这些属性.要获取别名,您需要先将普通路径强制转换为 POSIX 文件,然后再将 POSIX 文件强制转换为别名.

"path of front document" returns the POSIX path of a file, which is just a plain string. To be able to get information on an item the Finder will want an alias reference to the file in question. When you pass a plain string it gets an error because a plain string won't have those properties. To get an alias, you need to coerce the plain path first to a POSIX file and then coerce the POSIX file to an alias.

除非你在别处定义了这些变量,否则你需要去掉{displayed_namename_extensionextension_hidden}中的下划线.当您查看保留了这些下划线的已编译"代码时,如下图所示:

Unless you have defined these variables elsewhere, you need to remove the underscores in {displayed_name, name_extension, extension_hidden}. When you look at the "compiled" code with those underscores left in, it looks like in the following image:

因此,AppleScript 将 displayed_name 解释为一个变量,而不是一个属性.现在,如果您在其他地方定义了这些变量,例如在属性中的脚本顶部,那很好.但如果没有,您需要删除下划线,因为 Finder 项目的属性名称中没有下划线.删除下划线后,颜色显示正确(属性为紫色,变量为绿色).

So, the AppleScript is interpreting displayed_name to be a variable, not a property. Now, that's fine if you've defined those variables elsewhere, such as at the top of your script in properties. But if not, you need to remove the underscores, as the property names of Finder items don't have underscores in them. When you remove the underscores, the coloring appears correct (properties are purple with the variables being green).

请注意,如果没有扩展名,它仍然不会为您提供文件名.为此,您需要使用 text n thru m

Note that that still won't give you the filename without an extension. To get that, you'd need to do something like I did in the added line using text n thru m

if (_extension ≠ missing value) then
    set baseName to text 1 thru -((length of _extension) + 2) of _filename
end if

这篇关于如何从 Omnigraffle 获取没有扩展名的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆