Last active
October 8, 2020 11:38
-
-
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)
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
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.)
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
How to Use:
Automator
.Run applescript
from the sidebar, then paste the script in and save.Automator
program because it will throw error to you.