美文网首页
AppleScript实现复制路径、从这里启动

AppleScript实现复制路径、从这里启动

作者: 十一岁的加重 | 来源:发表于2018-06-26 21:50 被阅读36次

    平时比较喜欢使用XtraFinder,有几个功能(标签页、拷贝路径、从这里启动、新建文件、显示与隐藏、返回上级目录)实在太方便,但是后面系统升级了,发现装了它,Finder老是有问题,卡死,得重启Finder
    所以我们得自己用AppleScript做服务放右键菜单自己实现

    image.png

    复制路径

    tell application "Finder" to set selectedItems to selection as alias list
    -- 没选择的话直接退出脚本
    if selectedItems is {} then return
    -- 获得所选文件/文件夹的父目录。
    set parentPath to do shell script "dirname " & quoted form of POSIX path of (item 1 of selectedItems)
    -- 用于拼接多个路径
    set pathData to ""
    
    -- 历遍选择的项目
    repeat with theItem in selectedItems
        set pathData to pathData & POSIX path of theItem & linefeed
    end repeat
    
    -- 发送到剪贴板
    set the clipboard to pathData
    
    

    从这里启动在上面的基础上调用了cd命令

    tell application "Finder" to set selectedItems to selection as alias list
    -- 没选择的话直接退出脚本
    if selectedItems is {} then return
    -- 获得所选文件/文件夹的父目录。
    set parentPath to do shell script "dirname " & quoted form of POSIX path of (item 1 of selectedItems)
    -- 用于拼接多个路径
    set pathData to ""
    
    -- 历遍选择的项目
    repeat with theItem in selectedItems
        -- 如果是目录
        if (isDirectory(theItem)) then
            --如果是目录,我们就直接用这个目录
            set pathData to pathData & POSIX path of theItem & linefeed
            -- 把目录最后面的那个/字符去掉
            set pathData to text 1 thru -2 of pathData
        else
            -- 如果是文件我们就拿到上一级的目录
            set pathData to pathData & POSIX path of theItem & linefeed
            set pathData to characters 1 thru -((offset of "/" in (reverse of items of pathData as string)) + 1) of pathData as string
            
            -- 下面两句获得各个文件的文件名(文件夹的话获取文件夹名),并换行拼接
            --set fileName to do shell script "basename " & quoted form of POSIX path of theItem
            --set pathData to pathData & fileName & linefeed
        end if
    end repeat
    
    -- 发送到剪贴板
    --set the clipboard to pathData
    tell application "Terminal"
        do script "cd " & pathData & "; clear;"
        tell application "System Events"
            tell process "Terminal"
                
                set frontmost to true
                
            end tell
        end tell
    end tell
    
    ---------------------------------------------------------
    on isDirectory(someItem)
        set filePosixPath to quoted form of (POSIX path of someItem)
        set fileType to (do shell script "file -b " & filePosixPath)
        if fileType ends with "directory" then return true
        return false
    end isDirectory
    
    

    相关文章

      网友评论

          本文标题:AppleScript实现复制路径、从这里启动

          本文链接:https://www.haomeiwen.com/subject/vuqvyftx.html