class Object

Public Instance Methods

get_current_path_of_finder() click to toggle source
# File bin/lida, line 89
def get_current_path_of_finder
  script = <<~EOS
    tell application "Finder"
      if (count of windows) is 0 then
        set CurrentPath to "nitneuq"
      else
        set CurrentPath to (POSIX path of (folder of the front window as alias))
      end if
    end tell
    return CurrentPath
  EOS

  path = AppleScript.execute(script)
  raise NoWindowError if path =~ /nitneuq/
  path
end
get_current_path_of_xcode(xcode) click to toggle source
# File bin/lida, line 73
def get_current_path_of_xcode(xcode)
  script = <<~EOS
    tell application "#{xcode}"
      set CurrentDocument to document 1 whose name ends with (word -1 of (get name of window 1))
      set CurrentPath to path of CurrentDocument
      return CurrentPath
    end tell
  EOS
  begin
    path = AppleScript.execute(script)
  rescue
    raise NoWindowError
  end
  path
end
is_terminal_or_iterm() click to toggle source
# File bin/lida, line 106
def is_terminal_or_iterm
  script = <<-eos
    set AppPath to (path to frontmost application as text)
    return AppPath
  eos
  if AppleScript.execute(script) =~ /Terminal.app/; 0
  elsif AppleScript.execute(script) =~ /iTerm.app/; 1
  else; -1
  end
end
open_path(path) click to toggle source
# File bin/lida, line 117
def open_path(path)
  path.chomp!
  path = File.split(path)[0] unless File.directory?(path)
  flag = is_terminal_or_iterm
  if flag == 0
    open_path_in_terminal path
  elsif flag == 1
    open_path_in_iterm path
  else
    puts_error 'Lida only supports iterm and terminal for now.'
  end
end
open_path_in_iterm(dir) click to toggle source
# File bin/lida, line 130
def open_path_in_iterm(dir)
  script = <<~EOS
    set dir to (quoted form of POSIX path of ("#{dir}"))
    set command to "clear; cd " & dir
    tell application "iTerm"
      select first window
      tell the first window
        create tab with default profile
        tell current session to write text command
      end tell
    end tell
  EOS

  begin
    AppleScript.execute(script)
  rescue
    puts_error 'Open path in iterm failed.'
  end
end
open_path_in_terminal(path) click to toggle source
# File bin/lida, line 150
def open_path_in_terminal(path)
  script = <<~EOS
    tell application "Terminal"
      activate
      tell application "System Events" to keystroke "t" using command down
        delay 1
      do script ("clear") in first window
      do script ("cd '#{path}'") in first window
     end tell
  EOS
  begin
    AppleScript.execute(script)
  rescue
    puts_error 'Open path in terminal failed.'
  end
end
puts_error(msg) click to toggle source
# File bin/lida, line 167
def puts_error(msg)
  puts '[lida]: ' + msg.red
end