首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >macOS终端(或AppleScript) -从坞站中删除文件夹

macOS终端(或AppleScript) -从坞站中删除文件夹
EN

Stack Overflow用户
提问于 2019-12-16 00:18:43
回答 1查看 483关注 0票数 1

我在Dock的右侧有一个文件夹,我希望能够通过命令行将其删除(我最终会将其添加到AppleScript脚本中)。

是否有用于从Dock中删除文件夹的命令行工具?

编辑:我找到了这个脚本,它通过右键单击一个标题为"Airdrop“的文件夹,单击”Options“,然后单击”Remove from Dock“,将其从我的dock中删除

代码语言:javascript
复制
tell application "System Events" to tell UI element "AirDrop" of list 1 of application process "Dock"
    perform action "AXShowMenu"
    click menu item "Options" of menu 1
    click menu item "Remove from Dock" of menu 1 of menu item "Options" of menu 1
end tell

但是,因为它使用UI脚本,所以在运行脚本时,将出现如下图所示的rick-click菜单。

当我运行脚本时,有什么方法可以防止这个菜单出现在我的屏幕上吗?

EN

回答 1

Stack Overflow用户

发布于 2019-12-16 12:33:38

下面的脚本可以从dock中移除命名文件夹的dock磁贴。目前,它被设置为删除Downloads文件夹的停靠磁贴,如果有的话。

代码语言:javascript
复制
use framework "Foundation"
use framework "AppKit"

property this : a reference to the current application

property NSArray : a reference to NSArray of this
property NSMutableDictionary : a reference to NSMutableDictionary of this
property NSPredicate : a reference to NSPredicate of this
property NSPropertyList : a reference to NSPropertyListSerialization of this
property NSRunningApplication : a reference to NSRunningApplication of this
property NSString : a reference to NSString of this

property BinaryPList : 200

property id : "com.apple.dock"
property file : "~/Library/Preferences/" & id & ".plist"
property key : "persistent-others"
property keys : [key, "tile-data", "file-label", "lowercaseString"]


removeDockTileFolder("Downloads")
if the result = true then return killDock()
false


to removeDockTileFolder(directory as text)
    local directory

    set filepath to (NSString's ¬
        stringWithString:(my file))'s ¬
        stringByStandardizingPath()

    tell (NSMutableDictionary's dictionaryWithContentsOfFile:filepath)
        tell valueForKeyPath_((NSArray's ¬
            arrayWithArray:(keys as list))'s ¬
            componentsJoinedByString:".")
            set N to |count|()
            set i to indexOfObject_((NSString's ¬
                stringWithString:directory)'s ¬
                lowercaseString())
            if i ≥ N then return false
        end tell

        valueForKey_(my key)'s removeObjectAtIndex:i

        tell (NSPropertyList's dataFromPropertyList:it ¬
            format:BinaryPList errorDescription:(missing value)) ¬
            to writeToFile:filepath atomically:yes
    end tell
end removeDockTileForFolder

to killDock()
    (runningApplicationsWithBundleIdentifier_(my id) ¬
        in the NSRunningApplication)'s terminate

    true
end killDock

成功时返回true;失败时返回false (提供的名称与任何文件夹停靠磁贴的名称都不匹配)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59345726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档