Skip to content

Instantly share code, notes, and snippets.

@kamontat
Last active October 8, 2020 11:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamontat/7d5887828ad9cd5e8e8ff9c8c71273c5 to your computer and use it in GitHub Desktop.
Save kamontat/7d5887828ad9cd5e8e8ff9c8c71273c5 to your computer and use it in GitHub Desktop.
The AppleScript service, For open the new tab (if window exist) or new window (if window not launch before)
on CD_to(theDir)
tell application "iTerm"
activate
set go_dir_clear to "cd " & theDir & "&& clear"
create window with default profile
tell current session of first window
write text go_dir_clear
end tell
end tell
end CD_to
on run {input, parameters}
tell application "Finder"
set my_file to first item of input
set filetype to (kind of (info for my_file))
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)
end run
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
on CD_to(theDir)
set go_dir to "cd " & theDir & " && clear"
-- check running name in `activity Monitor` this must matching.
set isRunning to is_running("iTerm2")
-- you can log the script using `display notification` command
-- display notification isRunning
tell application "iTerm"
if isRunning then
tell current window
create tab with default profile
end tell
end if
tell current session of first window
write text go_dir
end tell
end tell
end CD_to
on run {input, parameters}
tell application "Finder"
set my_file to first item of input
set is_folder to (do shell script "file -b " & quoted form of (POSIX path of my_file))
if is_folder ends with "directory" 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)
end run
@kamontat
Copy link
Author

kamontat commented Feb 25, 2017

How 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 the script in and save.
  5. The service will automaticlly import to the finder menu bar (right click to open menu bar).
  • you don't need to run in the Automator program because it will throw error to you.

@kamontat
Copy link
Author

ChangeLog:

  • clean some code
  • add 2 type of the service (new window and new tab)

Important: I not sure that I already fix the bug that causes error when you try to use the service when iTerm close (but not quit).
(I can't testing it.)

@Reapor-Yurnero
Copy link

The action “Run AppleScript” encountered an error.

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