Skip to content

Instantly share code, notes, and snippets.

@redoPop
Last active May 17, 2024 04:04
Show Gist options
  • Save redoPop/72abb32b76ec0932289c to your computer and use it in GitHub Desktop.
Save redoPop/72abb32b76ec0932289c to your computer and use it in GitHub Desktop.
Alfred workflow to show file actions for an open document.
on alfred_script()
-- Replace with path to dir where your nvALT notes are kept as individual files
set nvFolder to "/Users/redoPop/Dropbox/Notes"
set docPath to false
try
tell application "System Events"
set frontApp to (name of first process whose frontmost is true)
end tell
-- Hat tip to Patrick Welker for this idea
if (frontApp is "Notational Velocity") or (frontApp is "nvALT") then
set nvFile to false
set nvExt to false
tell application "System Events" to tell process "nvALT"
set nvNote to value of text field 1 of group 1 of toolbar 1 of window 1
set nvFile to do shell script "printf %q " & quoted form of nvNote
end tell
set nvPath to nvFolder & POSIX path of nvFile
try
do shell script "ls " & nvPath & ".md"
set nvExt to ".md"
end try
if nvExt is false then
try
do shell script "ls " & nvPath & ".txt"
set nvExt to ".txt"
end try
end if
if nvExt is false then
try
do shell script "ls " & nvPath & ".taskpaper"
set nvExt to ".taskpaper"
end try
end if
if nvExt is false then
else
set docPath to (nvFolder & (POSIX path of nvNote) & nvExt)
end if
else
tell application (path to frontmost application as text)
set docPath to (path of document 1) as text
end tell
end if
on error
try
tell application "System Events" to tell (process 1 where frontmost is true)
value of attribute "AXDocument" of window 1
end tell
set docPath to do shell script "x=" & quoted form of result & "
x=${x/#file:\\/\\/}
x=${x/#localhost} # 10.8 and earlier
printf ${x//%/\\\\x}
"
end try
end try
-- The rest of this is Alfred-specific
if docPath is false then
return "No document was found in the active window."
else
-- Search for the file
tell application "Alfred 3"
search docPath
end tell
-- Show file actions
tell application "System Events"
-- Press "control" to show file actions
key code 59
end tell
end if
end alfred_script
@fractaledmind
Copy link

Lines 43-6, try this instead:

if nvExt is not false then
    set docPath to (nvFolder & (POSIX path of nvNote) & nvExt)
end if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment