1# -*- mode:ruby; indent-tabs-mode:nil; coding:utf-8 -*- 2# 3# VPRubyPlugin.rb 4# RubyPluginEnabler 5# 6# Created by Fujimoto Hisa on 07/02/02. 7# Copyright (c) 2007 FOBJ SYSTEMS. All rights reserved. 8 9require 'osx/cocoa' 10require 'VPRubyScript' 11 12class VPRubyPlugin < OSX::NSObject 13 include OSX 14 15 def self.logger=(log) @@logger = log end 16 def self.loginfo(fmt, *args) @@logger.info(fmt, *args) end 17 def self.logerror(err) @@logger.error(err) end 18 19 def self.install(enabler) 20 if not defined? @@instance then 21 @@instance = self.alloc.initWithEnabler(enabler) 22 end 23 end 24 25 def initWithEnabler(enabler) 26 @scripts = [] 27 @enabler = enabler 28 load_scripts 29 install_ruby_menu 30 install_menu 31 return self 32 end 33 34 def manager; @enabler.pluginManager end 35 36 def savePanelDidEnd_returnCode_contextInfo(savePanel, returnCode, windowController) 37 if returnCode == OSX::NSOKButton then 38 s = windowController.textView.string 39 s.dataUsingEncoding(OSX::NSUTF8StringEncoding). 40 writeToFile_atomically(savePanel.filename, true) 41 VPRubyScript.load(savePanel.filename.to_s).install_menu(manager) 42 # privateness, please ignore. 43 # [[self pluginManager] performSelector:@selector(sortMenu)]; 44 end 45 end 46 objc_method :savePanelDidEnd_returnCode_contextInfo, "v@:@i@" 47 48 private 49 50 def loginfo(fmt, *args) VPRubyPlugin.loginfo(fmt, *args) end 51 def logerror(err) VPRubyPlugin.logerror(err) end 52 53 def load_scripts 54 collect_scripts.each do |path| 55 begin 56 @scripts << VPRubyScript.load(path) 57 rescue Exception => err 58 logerror(err) 59 end 60 end 61 end 62 63 def install_ruby_menu 64 run_page_as_script.install_menu(manager) 65 save_page_as_script.install_menu(manager) 66 end 67 68 def install_menu 69 @scripts.each { |i| i.install_menu(manager) } 70 end 71 72 def run_page_as_script 73 VPRubyScript.create( 74 :superMenuTitle => "Ruby", 75 :menuTitle => "Run Page as Ruby Plugin") do 76 77 |windowController| 78 79 buffer = windowController.textView.textStorage.mutableString 80 r = windowController.textView.selectedRange 81 if r.length > 0 then 82 buffer = buffer.substringWithRange(r) 83 # now put the insertion point at the end of the selection. 84 r.location += r.length 85 r.length = 0 86 windowController.textView.setSelectedRange(r) 87 end 88 script = VPRubyScript.parse(buffer.to_s) 89 script.execute(windowController) 90 end 91 end 92 93 def save_page_as_script 94 VPRubyScript.create( 95 :superMenuTitle => "Ruby", 96 :menuTitle => "Save Page as Ruby Plugin...") do 97 98 |windowController| 99 100 # OSX.NSRunAlertPanel(SavePageAsScript.menuTitle, "not implement yet", nil, nil, nil) 101 102 key = windowController.key 103 displayName = windowController.document.vpDataForKey(key).displayName 104 105 if not displayName then 106 OSX.NSLog("I could not figure out the name of this page!") 107 OSX.NSBeep 108 break # exit from the action 109 end 110 111 name = "#{displayName}.rb" 112 113 savePanel = OSX::NSSavePanel.savePanel 114 115 savePanel.setPrompt(OSX.NSLocalizedString("Save", "Save")) 116 savePanel.setTitle(OSX.NSLocalizedString("Save as Ruby Plugin", "Save as Ruby Plugin")) 117 savePanel. 118 objc_send( :beginSheetForDirectory, path_to_user_scripts, 119 :file, name, 120 :modalForWindow, windowController.window, 121 :modalDelegate, self, 122 :didEndSelector, 'savePanelDidEnd:returnCode:contextInfo:', 123 :contextInfo, windowController ) 124 end 125 end 126 127 def collect_scripts 128 script_pathes.map{|path| Dir["#{path}/*.rb"] }.flatten 129 end 130 131 def script_pathes 132 [ path_to_bundle_scripts, path_to_user_scripts ] 133 end 134 135 def path_to_bundle_scripts 136 bundle = OSX::NSBundle.bundleForClass(self.class) 137 "#{bundle.resourcePath.to_s}/Script PlugIns" 138 end 139 140 def path_to_user_scripts 141 path = "~/Library/Application Support/VoodooPad/Script PlugIns" 142 path = File.expand_path(path) 143 require 'fileutils' 144 FileUtils.mkdir_p(path) if not File.exist?(path) 145 return path 146 end 147end 148