33

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

2
  • 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: apple.stackexchange.com/questions/26021/…
    – Sairam
    Mar 30 at 14:09
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
  • 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
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..

1
  • 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. Mar 17 at 4:53
3

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

1
  • 2
    I wish they would add that. Until then, scp will continue to be my friend.
    – Cajunluke
    Sep 20 '12 at 22:45
3

There is now a reverse engineered open-source version of AirDrop known as OpenDrop that wil run on MacOS and Linux - it allows for sending and receiving of files on the comand 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 reciever 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
3

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>

0
osascript -e 'tell application "Finder"
    activate
    tell application "System Events" to keystroke "R" using {command down, shift down}
end tell'
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .