Skip to content

Instantly share code, notes, and snippets.

@liuyigh
Last active December 13, 2018 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuyigh/85502c1cc565d16da8e9895b16d58574 to your computer and use it in GitHub Desktop.
Save liuyigh/85502c1cc565d16da8e9895b16d58574 to your computer and use it in GitHub Desktop.
iPyNbViewer: Standalone App for IPython Notebook (v3; latest)
on open this_item
tell application "Finder"
set pathList to (quoted form of POSIX path of (folder of the front window as alias))
set command to "clear; cd " & pathList
end tell
tell application "System Events"
-- some versions might identify as "iTerm2" instead of "iTerm"
set isRunning to (exists (processes where name is "iTerm")) or (exists (processes where name is "iTerm2"))
end tell
tell application "iTerm"
activate
set hasNoWindows to ((count of windows) is 0)
if isRunning and hasNoWindows then
create window with default profile
end if
select first window
tell the first window
if isRunning and hasNoWindows is false then
create tab with default profile
end if
tell current session
set name to "Jupyter Notebook"
write text "jupyter notebook " & (quoted form of POSIX path of this_item)
end tell
end tell
end tell
end open
on run
tell application "Finder"
if exists Finder window 1 then
set pathList to (quoted form of POSIX path of (folder of the front window as alias))
else
set pathList to desktop as alias
end if
set command to "clear; cd " & pathList
end tell
tell application "System Events"
-- some versions might identify as "iTerm2" instead of "iTerm"
set isRunning to (exists (processes where name is "iTerm")) or (exists (processes where name is "iTerm2"))
end tell
set choice to display dialog ("Shall we select a file or folder?") ¬
buttons {"Cancel", "File", "Folder"} with icon note ¬
cancel button 1 default button 2
if button returned of choice is "File" then
set ipynb to choose file
tell application "iTerm"
activate
set hasNoWindows to ((count of windows) is 0)
if isRunning and hasNoWindows then
create window with default profile
end if
select first window
tell the first window
if isRunning and hasNoWindows is false then
create tab with default profile
end if
tell current session
set name to "Jupyter Notebook"
write text "jupyter notebook " & (quoted form of POSIX path of ipynb)
end tell
end tell
end tell
else
set ipynbf to choose folder
tell application "iTerm"
activate
set hasNoWindows to ((count of windows) is 0)
if isRunning and hasNoWindows then
create window with default profile
end if
select first window
tell the first window
if isRunning and hasNoWindows is false then
create tab with default profile
end if
tell current session
set name to "Jupyter Notebook"
write text "cd " & (quoted form of POSIX path of ipynbf)
write text "jupyter notebook"
end tell
end tell
end tell
end if
end run
@valerie-osmobot
Copy link

Hi! Just as an FYI, I use this script frequently (it's so helpful, thank you for creating it!), but I recently upgraded to Mojave and it stopped working for a bit when I did.

I got this error: Not authorized to send Apple events to Finder. (-1743)
which led me to this thread: macOS Mojave, Automator “Not authorized to send Apple events to System Events.”

Following the thread seems to have resolved my errors.

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