Is there a way to share a file over AirDrop via Terminal on Mountain Lion? How so?
-
In what way? To send or receive files? It's finder functionality so to be honest I doubt it, but there maybe some AppleScript hyjinx depending on what you are trying to do.– bugglesCommented Aug 28, 2012 at 17:05
-
duplicate question: apple.stackexchange.com/questions/26021/…– SairamCommented Mar 30, 2021 at 14:09
8 Answers
I have not (yet) figured out a way to send files via Airdrop using the Terminal, but you can set your computer to receive files via Airdrop using the following shell script:
#!/usr/bin/osascript
tell application "Finder"
activate
tell application "System Events" to keystroke "R" using {command down, shift down}
end tell
-
1This would be really handy if you didn't need to accept each transfer. Perhaps that could be scripted as well.– bmike ♦Commented Sep 20, 2012 at 22:54
There is now a reverse engineered open-source version of AirDrop known as OpenDrop that runs on MacOS and Linux - it allows for sending and receiving of files on the command line with other AirDrop/OpenDrop capable devices (e.g. OpenDrop can run on a Raspberry Pi). To send you need to first discover the available devices e.g.
opendrop find
The chosen receiver is selected (by index
, ID
or name
) and a file is sent:
opendrop -r id -f /path/to/some/file
File reception is more straightforward - files will be downloaded into the current directory:
opendrop receive
There is a tool on Github called terminal-share that claims to offer this functionality:
terminal-share -service airdrop -image path/to/image.jpg
But it's not clear to me how you specify a destination..
-
I gave up on that tool long ago but gave it another try and found this solution to the bug. github.com/mattt/terminal-share/issues/… I use Mission Control to get the always-on-bottom (bug) share window onto its own desktop where I can interact with it. Commented Mar 17, 2021 at 4:53
You can do so using Hammerspoon. It's not completely automatic since you still have to select your phone.
opendrop doesn't work on the iPhone unfortunately.
I've written a blog post about this
Add the following to your init.lua
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
hs.urlevent.bind("airdrop", function(eventName, params)
local file = unescape(params["f"])
print(file)
local url = hs.sharing.fileURL(file)
local ad = hs.sharing.builtinSharingServices.sendViaAirDrop
local s = hs.sharing.newShare(ad)
s:shareItems({url})
end)
This will construct an endpoint to the hammerspoon Custom URL that can receive filenames as input.
Then you can create a script like this
#! /usr/bin/bash
rawurlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
REPLY="${encoded}"
}
rawurlencode "$@"
/usr/bin/open -g "hammerspoon://airdrop?f=${REPLY}"
This will URL-encode the input and send it to the airdrop
custom URL which we've registered with hammerspoon.
Save this somewhere as airdrop
file in your $PATH
, chmod +x
it (make executable), and then you can airdrop <myfile>
I created a CLI tool a few years ago that does what you want: https://github.com/vldmrkl/airdrop-cli
Hope this helps
No - the functionality is programmed into the core OS and lacks official hooks supplied by Apple via the command line.
-
2I wish they would add that. Until then,
scp
will continue to be my friend. Commented Sep 20, 2012 at 22:45
You can also automatically set your AirDrop receiving setting to "Everyone", script based on None of the above's answer:
#!/usr/bin/osascript
tell application "Finder"
make new Finder window
activate
end tell
tell application "System Events" to tell process "Finder"
keystroke "R" using {command down, shift down}
# open the "Allow me to be discovered by" dropdown
click button 1 of splitter group 1 of splitter group 1 of window 1
# select the 3rd option (Everyone)
click radio button 3 of pop over 1 of splitter group 1 of splitter group 1 of window 1
end tell
-
Ah yes, that does it! But how does one know IDs/names of the GUI controls, such as
splitter group 1
(and their nesting level)? Is there some debugger/inspector for Mac OS windows controls?– retifCommented Sep 14, 2023 at 12:53 -
osascript -e 'tell application "Finder"
activate
tell application "System Events" to keystroke "R" using {command down, shift down}
end tell'