【问题标题】:applescript get rid of full pathapplescript摆脱完整路径
【发布时间】:2011-11-28 16:04:31
【问题描述】:

我的 AppleScript 中有一个 choose file。当我运行脚本并选择一个文件时,输出总是带有文件扩展名的完整文件路径。例如:

Macintosh HD:Developer:About Xcode.pdf

是我不想要的。我只想要:

About Xcode


Kassym Dorsel 的以下答案在其中有多个 . 时不起作用。

Lri 的以下回答不适用于set x to choose file

error "Can’t make quoted form of alias \"Macintosh HD:Applications:Firefox.app:\" into        type Unicode text." number -1700 from quoted form of alias "Macintosh HD:Applications:Firefox.app:" to Unicode text

【问题讨论】:

    标签: macos applescript finder


    【解决方案1】:

    您可以使用 Finder 来操作 Finder 项目的名称:

    choose file with prompt "Pick one"
    set filepath to result
    
    tell application "Finder" to set {dispName, nameExt, isHidden} to ¬
        the {displayed name, name extension, extension hidden} of the filepath
    
    
    if isHidden or nameExt is equal to "" then
        dispName
    else
        (characters 1 through (-2 - (count of nameExt)) of dispName) as text
    end if
    
    set baseName to result
    

    【讨论】:

    • @Lri 感谢您了解有关扩展的信息;固定的。由于该文件是使用choose file 选择的,因此该文件将始终存在——您需要使用choose file name 来获取对可能不存在的文件的引用。
    【解决方案2】:

    这将起作用:

    set a to "Macintosh HD:Developer:About.Xcode.pdf"
    set text item delimiters to ":"
    set temp to last text item of a
    set text item delimiters to "."
    set temp to text items 1 thru -2 of temp as text
    

    给 => About.Xcode

    【讨论】:

    • 当文件名包含多个. 字符时,此操作将失败。
    • 我最初的解决方案是针对具体情况的,是的。我已更新我的解决方案以允许文件名中包含句点。它现在应该适用于一切。
    猜你喜欢
    • 1970-01-01
    • 2016-09-14
    • 2021-05-05
    • 1970-01-01
    • 2019-06-30
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    相关资源
    最近更新 更多