AppleScript 如何将文件复制到系统路径
AppleScript How to copy file to system path
我写一个 AppleScript
do shell script "mv /Users/xxx/Documents/1.png /usr/local/"
with administrator privileges
但它失败了,return“不允许操作”。
我知道 Apple 不允许将文件复制到 /usr/local。但是,如果我将目标文件夹从“/usr/local/”更改为“/Users/xxx/Desktop/”,脚本也会失败。
另外,像这样的脚本都成功了。 ↓↓↓
do shell script "touch /usr/local/1.txt" with administrator privileges
或
do shell script "mkdir /usr/local/testfolder" with administrator privileges
为什么创建文件或文件夹最后成功了,但是复制文件却失败了?
显示的两种情况(“/usr/local/”和桌面)在新系统上受到不同的保护,对其行为有不同的解释。
桌面。要使用 "mv" 命令将文件移动到桌面,您只需授予此命令对桌面的访问权限(或完整的磁盘访问权限)。因为它是一个非系统位置,尽管有一些保护。
"/usr/local/"文件夹。这是一个 SIP 保护的系统文件夹。因此,仅授予访问权限在这里无济于事。通常建议通过重新启动两次计算机等来暂时禁用 SIP。
我个人认为这个建议很糟糕。最好放弃“mv”命令,让系统自己移动只允许它移动的东西。
set thePNG to (choose file of type "png") as text
set usrLocalFolder to ((path to (startup disk)) as text) & "usr:local"
tell application "Finder" to move file thePNG to folder usrLocalFolder
-- tell application "Finder" to move file thePNG to desktop
在最新版本的 macOS 中,不知道有多远,如果你想 copy/move files/folders 从 Home folder 的默认 folders 到例如/usr/local
使用 do shell script
命令 例如脚本编辑器,然后它需要首先 copied/moved 到不在默认 文件夹中的 文件夹 您的 主页 文件夹.
换句话说,如果 files/folders 在您的 Home[=] 的 root 中87=] 文件夹,Public 文件夹,或 user 在您的 Home folder 中创建了 folder,那么这不是问题。但是,如果它位于 Desktop、Documents、Downloads、Library、电影、音乐、照片或图片,那么 files/folders 需要先在其他地方 copied/moved 然后再到 /usr/local
.
以下示例 AppleScript 代码 对我有用:
do shell script "mv ~/Documents/1.png ~/Public/"
do shell script "mv ~/Public/1.png /usr/local/" with administrator privileges
备注:
在 scripts 中我需要这样做我首先 copy/move files/folders /private/tmp/
因为我确定我不会不小心覆盖我 主页 文件夹.
中其他地方的内容
也就是说,虽然我当然可以看到想要在 Finder 上使用 do shell script
命令 的原因,但是, Finder 是另一个答案中提到的可行选项。
上面显示的示例 AppleScript 代码在[=52=中进行了测试macOS Catalina 下的 ]Script Editor 以及 System Preferences 中的 Language & Region 设置到 英语(美国)— 小学 并且毫无问题地为我工作1.
- 1 在 系统偏好设置 > 安全与隐私[=] 中假定必要且适当的设置87=] > 隐私 已根据需要 set/addressed。
我写一个 AppleScript
do shell script "mv /Users/xxx/Documents/1.png /usr/local/" with administrator privileges
但它失败了,return“不允许操作”。
我知道 Apple 不允许将文件复制到 /usr/local。但是,如果我将目标文件夹从“/usr/local/”更改为“/Users/xxx/Desktop/”,脚本也会失败。
另外,像这样的脚本都成功了。 ↓↓↓
do shell script "touch /usr/local/1.txt" with administrator privileges
或
do shell script "mkdir /usr/local/testfolder" with administrator privileges
为什么创建文件或文件夹最后成功了,但是复制文件却失败了?
显示的两种情况(“/usr/local/”和桌面)在新系统上受到不同的保护,对其行为有不同的解释。
桌面。要使用 "mv" 命令将文件移动到桌面,您只需授予此命令对桌面的访问权限(或完整的磁盘访问权限)。因为它是一个非系统位置,尽管有一些保护。
"/usr/local/"文件夹。这是一个 SIP 保护的系统文件夹。因此,仅授予访问权限在这里无济于事。通常建议通过重新启动两次计算机等来暂时禁用 SIP。
我个人认为这个建议很糟糕。最好放弃“mv”命令,让系统自己移动只允许它移动的东西。
set thePNG to (choose file of type "png") as text
set usrLocalFolder to ((path to (startup disk)) as text) & "usr:local"
tell application "Finder" to move file thePNG to folder usrLocalFolder
-- tell application "Finder" to move file thePNG to desktop
在最新版本的 macOS 中,不知道有多远,如果你想 copy/move files/folders 从 Home folder 的默认 folders 到例如/usr/local
使用 do shell script
命令 例如脚本编辑器,然后它需要首先 copied/moved 到不在默认 文件夹中的 文件夹 您的 主页 文件夹.
换句话说,如果 files/folders 在您的 Home[=] 的 root 中87=] 文件夹,Public 文件夹,或 user 在您的 Home folder 中创建了 folder,那么这不是问题。但是,如果它位于 Desktop、Documents、Downloads、Library、电影、音乐、照片或图片,那么 files/folders 需要先在其他地方 copied/moved 然后再到 /usr/local
.
以下示例 AppleScript 代码 对我有用:
do shell script "mv ~/Documents/1.png ~/Public/"
do shell script "mv ~/Public/1.png /usr/local/" with administrator privileges
备注:
在 scripts 中我需要这样做我首先 copy/move files/folders /private/tmp/
因为我确定我不会不小心覆盖我 主页 文件夹.
也就是说,虽然我当然可以看到想要在 Finder 上使用 do shell script
命令 的原因,但是, Finder 是另一个答案中提到的可行选项。
上面显示的示例 AppleScript 代码在[=52=中进行了测试macOS Catalina 下的 ]Script Editor 以及 System Preferences 中的 Language & Region 设置到 英语(美国)— 小学 并且毫无问题地为我工作1.
- 1 在 系统偏好设置 > 安全与隐私[=] 中假定必要且适当的设置87=] > 隐私 已根据需要 set/addressed。