-
-
Save cdzombak/ae57dff167f2352f0845b0b3a35b8258 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -eo pipefail | |
# ref: http://hints.macworld.com/article.php?story=20040111200114634 | |
projectName="$(osascript <<'END' | |
on trim_line(this_text, trim_chars, trim_indicator) | |
-- 0 = beginning, 1 = end, 2 = both | |
set x to the length of the trim_chars | |
-- TRIM BEGINNING | |
if the trim_indicator is in {0, 2} then | |
repeat while this_text begins with the trim_chars | |
try | |
set this_text to (characters (x + 1) thru -1 of this_text) as string | |
on error | |
-- the text contains nothing but the trim characters | |
return "" | |
end try | |
end repeat | |
end if | |
-- TRIM ENDING | |
if the trim_indicator is in {1, 2} then | |
repeat while this_text ends with the trim_chars | |
try | |
set this_text to (characters 1 thru -(x + 1) of this_text) as string | |
on error | |
-- the text contains nothing but the trim characters | |
return "" | |
end try | |
end repeat | |
end if | |
return this_text | |
end trim_line | |
if the clipboard does not start with "bear://" then | |
error "The clipboard does not contain a Bear link." | |
end if | |
set previousClipboard to the clipboard | |
tell application "Bear" to activate | |
tell application "System Events" | |
tell process "Bear" | |
keystroke "c" using command down | |
keystroke "h" using command down | |
end tell | |
end tell | |
delay 0.2 | |
set theParagraphs to paragraphs of (the clipboard) | |
set the clipboard to previousClipboard | |
-- project name is the first line, minus Markdown header | |
set theProjectName to trim_line(item 1 of theParagraphs, "# ", 0) | |
return theProjectName | |
END | |
)" | |
fname=$(echo "$projectName" | tr -cd 'A-Za-z0-9_- ' | awk '{$1=$1};1') | |
url="$(pbpaste)" | |
# if we couldn't use page title as filename, fallback to a stripped-down version of the URL: | |
if [ "$fname" == "" ]; then | |
fname=$(echo "$url" | tr -cd 'A-Za-z0-9_- ' | awk '{$1=$1};1') | |
fi | |
# check if the file exists already: | |
fname="./""$fname"".url" | |
if [ -e "$fname" ] && [ "$force" != true ] ; then | |
echo "error: file '$fname' exists" | |
exit 2 | |
fi | |
fname="$1/$fname" | |
cat << EOF > "$fname" | |
[InternetShortcut] | |
URL=$url | |
EOF | |
osascript <<END | |
use framework "Foundation" | |
use framework "AppKit" | |
use scripting additions | |
set sourcePath to "/Applications/Bear.app/Contents/Resources/Bear Document.icns" | |
set destPath to "$fname" | |
set imageData to (current application's NSImage's alloc()'s initWithContentsOfFile:sourcePath) | |
(current application's NSWorkspace's sharedWorkspace()'s setIcon:imageData forFile:destPath options:2) | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment