Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/platform/darwin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,35 @@
# This file is licensed under the GPLv2+. Please see COPYING for more information.

clip() {
copy_concealed() {
if command -v osascript &>/dev/null; then
osascript -l JavaScript \
-e 'ObjC.import("AppKit")' \
-e 'ObjC.import("Foundation")' \
-e 'const input = $.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile' \
-e 'const text = $.NSString.alloc.initWithDataEncoding(input, $.NSUTF8StringEncoding)' \
-e 'const pasteboard = $.NSPasteboard.generalPasteboard' \
-e 'pasteboard.clearContents' \
-e 'pasteboard.setStringForType(text, $.NSPasteboardTypeString)' \
-e 'pasteboard.setDataForType($.NSData.data, "org.nspasteboard.ConcealedType")' \
-e 'pasteboard.setDataForType($.NSData.data, "org.nspasteboard.TransientType")' \
-e 'pasteboard.setDataForType($.NSData.data, "org.nspasteboard.AutoGeneratedType")' \
-e 'undefined'
else
pbcopy
fi
}

local sleep_argv0="password store sleep for user $(id -u)"
pkill -f "^$sleep_argv0" 2>/dev/null && sleep 0.5
local before="$(pbpaste | $BASE64)"
echo -n "$1" | pbcopy
local secret="$(printf "%s" "$1" | $BASE64)"
printf "%s" "$1" | copy_concealed || die "Error: could not copy password to clipboard."
(
( exec -a "$sleep_argv0" sleep "$CLIP_TIME" )
local now="$(pbpaste | $BASE64)"
[[ $now != $(echo -n "$1" | $BASE64) ]] && before="$now"
echo "$before" | $BASE64 -d | pbcopy
[[ $now != "$secret" ]] && exit
printf "%s\n" "$before" | $BASE64 -d | copy_concealed
) >/dev/null 2>&1 & disown
echo "Copied $2 to clipboard. Will clear in $CLIP_TIME seconds."
}
Expand Down