Skip to content

Instantly share code, notes, and snippets.

@fumiyas
Last active May 10, 2018 11:32
Show Gist options
  • Select an option

  • Save fumiyas/bbe8741cbc68da3caafe0590e1287c87 to your computer and use it in GitHub Desktop.

Select an option

Save fumiyas/bbe8741cbc68da3caafe0590e1287c87 to your computer and use it in GitHub Desktop.
Copy and remove a file or directory by macOS Finder application
#!/bin/bash
##
## Copy a file or directory by macOS Finder application
## Copyright (c) 2018 SATOH Fumiyasu @ OSS Technology Corp., Japan
##
## License: GNU General Public License version 3
##
set -u
if [[ $# -ne 2 ]]; then
echo "Usage: $0 SRC DST"
exit 1
fi
path2abs() {
if [[ $1 != /* ]]; then
printf '%s' "$PWD/$1"
else
printf '%s' "$1"
fi
}
src=$(path2abs "$1"); shift
dst=$(path2abs "$1"); shift
/usr/bin/osascript - "$src" "$dst" <<'EOF'
on run argv
set srcPath to POSIX file (item 1 of argv)
set dstDir to POSIX file (item 2 of argv)
with timeout of 0 seconds
tell application "Finder"
#activate
duplicate srcPath to folder dstDir
end tell
end timeout
end run
EOF
#!/bin/bash
##
## Remove a file or directory by macOS Finder application
## Copyright (c) 2018 SATOH Fumiyasu @ OSS Technology Corp., Japan
##
## License: GNU General Public License version 3
##
set -u
if [[ $# -ne 1 ]]; then
echo "Usage: $0 TARGET"
exit 1
fi
path2abs() {
if [[ $1 != /* ]]; then
printf '%s' "$PWD/$1"
else
printf '%s' "$1"
fi
}
target=$(path2abs "$1"); shift
/usr/bin/osascript - "$target" <<'EOF'
on run argv
set targetPath to POSIX file (item 1 of argv)
with timeout of 0 seconds
tell application "Finder"
#activate
if kind of (info for targetPath) is "folder" then
delete (files of folder targetPath)
end if
delete targetPath
end tell
end timeout
end run
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment