Last active
June 13, 2020 02:59
-
-
Save Teraflopst/29004aafc1d9d1cf316b940b0abf060d to your computer and use it in GitHub Desktop.
AppleScript:获取选中文件(夹)的目录路径
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 脚本介绍 | |
-- 功能:获取所选文件、文件夹的目录路径,支持多选 | |
-- 如果仅仅想获取文件的目录路径,可以调用 isDirectory() 过滤掉文件夹的路径 | |
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 | |
-- 下面两句获得各个文件的文件名(文件夹的话获取文件夹名),并换行拼接 | |
-- set fileName to do shell script "basename " & quoted form of POSIX path of theItem | |
-- set pathData to pathData & fileName & linefeed | |
end repeat | |
-- 发送到剪贴板 | |
set the clipboard to pathData | |
--------------------------------------------------------- | |
-- 函数:判断所选文件是否是文件夹 | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 脚本介绍 | |
-- 功能:获取所选项文件的目录路径,支持多选,过滤掉文件夹路径 | |
-- 如果想同时获取文件夹的目录路径,可以去除 isDirectory() 判断 | |
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 (not isDirectory(theItem)) then | |
-- 获得各个选中项的路径,换行拼接 | |
set pathData to pathData & POSIX path of theItem & linefeed | |
-- 下面两句获得各个文件的文件名(文件夹的话获取文件夹名),并换行拼接 | |
-- 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 | |
--------------------------------------------------------- | |
-- 函数:判断所选文件是否是文件夹 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment