Skip to content

Instantly share code, notes, and snippets.

@allthesignals
Forked from eric-hu/Open iterm tab here
Last active April 7, 2016 15:24
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 allthesignals/aafd75148fed34e8c67a5b57963996ee to your computer and use it in GitHub Desktop.
Save allthesignals/aafd75148fed34e8c67a5b57963996ee to your computer and use it in GitHub Desktop.
Apple script to right click a CSV file to open it in an interactive python shell and load into a Pandas dataframe. To use:(1) Open Automator(2) Create a new service(3) Change "Service receives selected" drop downs to "Files or folders" in "Finder"(4) Select "Run applescript" from the sidebar, then paste this script in and save
-- Adapted from these sources:
-- http://peterdowns.com/posts/open-iterm-finder-service.html
-- https://gist.github.com/cowboy/905546
--
-- Modified to work with files as well, cd-ing to their container folder
on run {input, parameters}
tell application "Finder"
set my_file to first item of input
set filetype to (kind of (info for my_file))
-- Treats OS X applications as files. To treat them as folders, integrate this SO answer:
-- http://stackoverflow.com/a/6881524/640517
if filetype is "Folder" or filetype is "Volume" then
set dir_path to quoted form of (POSIX path of my_file)
else
set dir_path to quoted form of (POSIX path of (container of my_file as string))
end if
end tell
CD_to(dir_path, input, filetype)
end run
on CD_to(theDir, input, filetype)
tell application "iTerm"
set my_file to first item of input
set my_file to quoted form of (POSIX path of my_file)
activate
try
set t to the last terminal
on error
set t to (make new terminal)
end try
tell t
launch session "Default Session"
tell the last session
write text "python3"
write text "import pandas as pd"
write text "file_path =" & my_file
write text "# " & filetype
if filetype is "Microsoft Excel Workbook (.xlsx)" then
write text "data = pd.read_excel(file_path)"
else
write text "data = pd.read_csv(file_path)"
end if
end tell
end tell
end tell
end CD_to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment