Skip to content

Commit

Permalink
feat(main): support Hyper
Browse files Browse the repository at this point in the history
- Support Hyper
- Give priority to creating a new tab when opening iTerm
- Slightly modified iTerm icons
  • Loading branch information
Ji4n1ng committed Apr 15, 2019
1 parent 8860d88 commit 8c9bfe4
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 7 deletions.
14 changes: 13 additions & 1 deletion OpenInTerminal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
956C38E2225F840F001CB769 /* icon_iterm_white.icns in Resources */ = {isa = PBXBuildFile; fileRef = 956C38DE225F840F001CB769 /* icon_iterm_white.icns */; };
956C38E3225F840F001CB769 /* icon_iterm_dark.icns in Resources */ = {isa = PBXBuildFile; fileRef = 956C38DF225F840F001CB769 /* icon_iterm_dark.icns */; };
956C38E4225F840F001CB769 /* icon_terminal_dark.icns in Resources */ = {isa = PBXBuildFile; fileRef = 956C38E0225F840F001CB769 /* icon_terminal_dark.icns */; };
95C6B9EC2264BCD40004709B /* HyperApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95C6B9EB2264BCD40004709B /* HyperApp.swift */; };
95C6B9F32264CA480004709B /* icon_hyper_dark.icns in Resources */ = {isa = PBXBuildFile; fileRef = 95C6B9F12264CA480004709B /* icon_hyper_dark.icns */; };
95C6B9F42264CA480004709B /* icon_hyper_white.icns in Resources */ = {isa = PBXBuildFile; fileRef = 95C6B9F22264CA480004709B /* icon_hyper_white.icns */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -39,6 +42,9 @@
956C38DE225F840F001CB769 /* icon_iterm_white.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = icon_iterm_white.icns; path = icons/icon_iterm_white.icns; sourceTree = SOURCE_ROOT; };
956C38DF225F840F001CB769 /* icon_iterm_dark.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = icon_iterm_dark.icns; path = icons/icon_iterm_dark.icns; sourceTree = SOURCE_ROOT; };
956C38E0225F840F001CB769 /* icon_terminal_dark.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = icon_terminal_dark.icns; path = icons/icon_terminal_dark.icns; sourceTree = SOURCE_ROOT; };
95C6B9EB2264BCD40004709B /* HyperApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HyperApp.swift; sourceTree = "<group>"; };
95C6B9F12264CA480004709B /* icon_hyper_dark.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = icon_hyper_dark.icns; path = icons/icon_hyper_dark.icns; sourceTree = SOURCE_ROOT; };
95C6B9F22264CA480004709B /* icon_hyper_white.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = icon_hyper_white.icns; path = icons/icon_hyper_white.icns; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -88,8 +94,9 @@
956C38C4225F7DB7001CB769 /* Terminals */ = {
isa = PBXGroup;
children = (
956C38CB225F7E45001CB769 /* iTermApp.swift */,
956C38CA225F7E45001CB769 /* TerminalApp.swift */,
956C38CB225F7E45001CB769 /* iTermApp.swift */,
95C6B9EB2264BCD40004709B /* HyperApp.swift */,
);
path = Terminals;
sourceTree = "<group>";
Expand All @@ -110,6 +117,8 @@
956C38DE225F840F001CB769 /* icon_iterm_white.icns */,
956C38E0225F840F001CB769 /* icon_terminal_dark.icns */,
956C38DD225F840F001CB769 /* icon_terminal_white.icns */,
95C6B9F12264CA480004709B /* icon_hyper_dark.icns */,
95C6B9F22264CA480004709B /* icon_hyper_white.icns */,
);
path = Resources;
sourceTree = "<group>";
Expand Down Expand Up @@ -180,6 +189,8 @@
files = (
956C38E4225F840F001CB769 /* icon_terminal_dark.icns in Resources */,
956C38E2225F840F001CB769 /* icon_iterm_white.icns in Resources */,
95C6B9F42264CA480004709B /* icon_hyper_white.icns in Resources */,
95C6B9F32264CA480004709B /* icon_hyper_dark.icns in Resources */,
956C38E3225F840F001CB769 /* icon_iterm_dark.icns in Resources */,
956C38B7225F7701001CB769 /* Assets.xcassets in Resources */,
956C38E1225F840F001CB769 /* icon_terminal_white.icns in Resources */,
Expand All @@ -197,6 +208,7 @@
956C38CC225F7E45001CB769 /* TerminalApp.swift in Sources */,
956C38C3225F793C001CB769 /* main.swift in Sources */,
956C38CD225F7E45001CB769 /* iTermApp.swift in Sources */,
95C6B9EC2264BCD40004709B /* HyperApp.swift in Sources */,
956C38C8225F7E30001CB769 /* Finder.swift in Sources */,
956C38D2225F7E76001CB769 /* Error.swift in Sources */,
956C38D3225F7E76001CB769 /* Log.swift in Sources */,
Expand Down
3 changes: 3 additions & 0 deletions OpenInTerminal/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum OITError: Error {
case cannotAccessFinder
case cannotAccessTerminal
case cannotAccessIterm
case cannotAccessHyper
case wrongUrl

}
Expand All @@ -29,6 +30,8 @@ extension OITError : CustomStringConvertible {
return "Cannot access Terminal, please check permissions."
case .cannotAccessIterm:
return "Cannot access iTerm, please check permissions."
case .cannotAccessHyper:
return "Cannot access Hyper, please check permissions."
case .wrongUrl:
return "Oops, got a wrong url"
}
Expand Down
2 changes: 1 addition & 1 deletion OpenInTerminal/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.1.0</string>
<string>0.1.1</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
Expand Down
1 change: 1 addition & 0 deletions OpenInTerminal/OpenInTerminal.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<string>com.apple.Terminal</string>
<string>com.apple.Finder</string>
<string>com.googlecode.iterm2</string>
<string>co.zeit.hyper</string>
</array>
</dict>
</plist>
43 changes: 43 additions & 0 deletions OpenInTerminal/Terminals/HyperApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// HyperApp.swift
// OpenInTerminal
//
// Created by Jianing Wang on 2019/4/15.
// Copyright © 2019 Jianing Wang. All rights reserved.
//

import Foundation

final class HyperApp : Terminal {

func open(_ path: String) throws {

guard let url = URL(string: path) else {
throw OITError.wrongUrl
}

let source = """
do shell script "open -a Hyper \(url.path.hyperEscaped)"
"""

let script = NSAppleScript(source: source)!

var error: NSDictionary?

script.executeAndReturnError(&error)

if error != nil {
log(error, .error)
throw OITError.cannotAccessHyper
}
}

}

extension String {

// FIXME: if path contains "\", application will crash.
var hyperEscaped: String {
return self.replacingOccurrences(of: " ", with: "\\\\ ")
}
}
26 changes: 22 additions & 4 deletions OpenInTerminal/Terminals/iTermApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,27 @@ final class iTermApp : Terminal {

let source = """
tell application "iTerm"
create window with default profile
tell current session of current window
write text "cd \(url.path.pathEscaped); clear"
set isRunning to (application "iTerm" is running)
activate
tell current window
if (count of tabs) < 1 then
create window with default profile
set isRunning to false
end if
if isRunning then
set newTab to (create tab with default profile)
tell newTab
select
end tell
end if
tell current session
write text "cd \(url.path.itermEscaped); clear"
end tell
end tell
end tell
"""
Expand All @@ -43,7 +61,7 @@ final class iTermApp : Terminal {
extension String {

// FIXME: if path contains "\", application will crash.
var pathEscaped: String {
var itermEscaped: String {

var result = ""
let set = CharacterSet.alphanumerics
Expand Down
2 changes: 1 addition & 1 deletion OpenInTerminal/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ do {
}

// TODO: User can choose which terminal to open
let term = iTermApp() // iTermApp() TerminalApp()
let term = TerminalApp() // TerminalApp() iTermApp() HyperApp()
try term.open(path)

} catch {
Expand Down
Binary file added icons/icon_hyper_dark.icns
Binary file not shown.
Binary file added icons/icon_hyper_white.icns
Binary file not shown.
Binary file modified icons/icon_iterm_dark.icns
Binary file not shown.
Binary file modified icons/icon_iterm_white.icns
Binary file not shown.

0 comments on commit 8c9bfe4

Please sign in to comment.