42

Is there a way to share a file over AirDrop via Terminal on Mountain Lion? How so?

myhd
  • 3,480
Moshe
  • 8,761
  • 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. – buggles Aug 28 '12 at 17:05
  • duplicate question: https://apple.stackexchange.com/questions/26021/airdrop-how-to-access-airdrop-from-command-line/367073 – Sairam Mar 30 '21 at 14:09

8 Answers8

8

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
  • 1
    This would be really handy if you didn't need to accept each transfer. Perhaps that could be scripted as well. – bmike Sep 20 '12 at 22:54
7

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
Pierz
  • 3,647
4

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. https://github.com/mattt/terminal-share/issues/11#issuecomment-800791854 I use Mission Control to get the always-on-bottom (bug) share window onto its own desktop where I can interact with it. – Bruno Bronosky Mar 17 '21 at 04:53
4

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>

lnaef
  • 41
3

I created a CLI tool a few years ago that does what you want: https://github.com/vldmrkl/airdrop-cli

Hope this helps

3

No - the functionality is programmed into the core OS and lacks official hooks supplied by Apple via the command line.

bmike
  • 235,889
1

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
sceid
  • 11
  • 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? – retif Sep 14 '23 at 12:53
  • ...found it - standard Accessibility Inspector tool. – retif Sep 14 '23 at 13:02
0
osascript -e 'tell application "Finder"
    activate
    tell application "System Events" to keystroke "R" using {command down, shift down}
end tell'
BabyBoy
  • 925