win32-shortcut 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +44 -0
- data/MANIFEST +11 -0
- data/README +61 -0
- data/install.rb +11 -0
- data/lib/win32/shortcut.rb +206 -0
- data/test/tc_shortcut.rb +109 -0
- data/win32-shortcut.gemspec +23 -0
- metadata +56 -0
data/CHANGES
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
== 0.2.0 - 30-Oct-2006
|
2
|
+
* Replaced the C extension code with a pure Ruby version that uses win32ole
|
3
|
+
and Wscript.Shell behind the scenes.
|
4
|
+
* The Shortcut#icon_number method was dropped since that property isn't
|
5
|
+
supported by Wscript.Shell. It was a useless method anyway.
|
6
|
+
* The Shortcut#relative_path method was dropped since that property isn't
|
7
|
+
actually supported by a Wsh.Shortcut object, contrary to what the MSDN
|
8
|
+
documentation says.
|
9
|
+
* The Shortcut.new method now takes a block, yields self, and automatically
|
10
|
+
ensures that Shortcut#save is called at the end of the block.
|
11
|
+
* The Shortcut#hotkey method now takes a human readable string instead of a
|
12
|
+
hex value.
|
13
|
+
* The Shortcut#show_cmd method was renamed Shortcut#window_style (though
|
14
|
+
there's an alias for backwards compatability.
|
15
|
+
* The Shortcut#window_style accessor now accepts/emits human readable
|
16
|
+
strings as well as constants.
|
17
|
+
* Removed the 'doc' directory. The documentation is now inlined as rdoc
|
18
|
+
and/or in the README file.
|
19
|
+
|
20
|
+
== 0.1.4 - 17-Jul-2005
|
21
|
+
* Fixed a bug in the Shortcut.open method. Thanks go to an anonymous user
|
22
|
+
for the spot.
|
23
|
+
* Added a test for the Shortcut.open method.
|
24
|
+
* Added a Shortcut.open example in the test script.
|
25
|
+
* Modified the extconf.rb script to set _WIN32_WINNT macro as needed.
|
26
|
+
|
27
|
+
== 0.1.3 - 9-Jun-2005
|
28
|
+
* Removed shortcut.rd file.
|
29
|
+
* The shortcut.txt file is now rdoc friendly.
|
30
|
+
* Now Unicode friendly.
|
31
|
+
* Minor test suite tweaks and additions.
|
32
|
+
* Minor changes to the example program.
|
33
|
+
* Code cleanup.
|
34
|
+
|
35
|
+
== 0.1.2 - 1-Mar-2005
|
36
|
+
* Moved the 'examples' directory to the toplevel directory.
|
37
|
+
* Renamed the sample script to 'shortcut_test.rb'.
|
38
|
+
|
39
|
+
== 0.1.1 - 12-Feb-2005
|
40
|
+
* Modified the extconf.rb file to be more cygwin/mingw friendly.
|
41
|
+
* Modified some documentation to be more rdoc friendly.
|
42
|
+
|
43
|
+
== 0.1.0 - 28-Sep-2004
|
44
|
+
* Initial release
|
data/MANIFEST
ADDED
data/README
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
== Description
|
2
|
+
This is an interface for creating and/or modifying Windows shortcuts.
|
3
|
+
|
4
|
+
== Requirements
|
5
|
+
Ruby 1.8.2 or later.
|
6
|
+
The win32ole package (part of the stdlib).
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
=== Gem Installation
|
10
|
+
* ruby win32-shortcut.gemspec
|
11
|
+
* gem install win32-shortcut-x.y.z.gem
|
12
|
+
=== Standard Installation
|
13
|
+
* ruby install.rb
|
14
|
+
|
15
|
+
== Synopsis
|
16
|
+
require 'win32/shortcut'
|
17
|
+
include Win32
|
18
|
+
|
19
|
+
Shortcut.new('c:\test.lnk') do |s|
|
20
|
+
s.description = "test link"
|
21
|
+
s.path = "c:\\winnt\\notepad.exe"
|
22
|
+
s.show_cmd = Shortcut::SHOWNORMAL
|
23
|
+
s.working_directory = "C:\\"
|
24
|
+
end
|
25
|
+
|
26
|
+
== Notes
|
27
|
+
This code uses win32ole + wscript behind the scenes. The original C code
|
28
|
+
is not distributed as part of the release, but is still retained in the
|
29
|
+
CVS repository under the 'ext' directory should anyone ever need it.
|
30
|
+
|
31
|
+
== Known Bugs
|
32
|
+
None known.
|
33
|
+
|
34
|
+
Please report any bugs on the project page at
|
35
|
+
http://rubyforge.org/projects/win32utils.
|
36
|
+
|
37
|
+
== Future Plans
|
38
|
+
Ideas welcome. Please submit them on the project page as a
|
39
|
+
"Feature Request".
|
40
|
+
|
41
|
+
== Copyright
|
42
|
+
(C) 2003-2006, Daniel J. Berger, All Rights Reserved
|
43
|
+
|
44
|
+
== License
|
45
|
+
Ruby's
|
46
|
+
|
47
|
+
== Warranty
|
48
|
+
This package is provided "as is" and without any express or
|
49
|
+
implied warranties, including, without limitation, the implied
|
50
|
+
warranties of merchantability and fitness for a particular purpose.
|
51
|
+
|
52
|
+
== Acknowledgements
|
53
|
+
The original win32-shortcut package was largely based on Aldo Calpini's
|
54
|
+
Win32::Shortcut Perl module, and I copied some of the documentation as
|
55
|
+
well.
|
56
|
+
|
57
|
+
The current version is based on a patch by Jano Svitok.
|
58
|
+
|
59
|
+
== Author(s)
|
60
|
+
Daniel J. Berger
|
61
|
+
Park Heesob
|
data/install.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# For those who don't like gems...
|
2
|
+
require 'rbconfig'
|
3
|
+
require 'ftools'
|
4
|
+
include Config
|
5
|
+
|
6
|
+
sitelibdir = CONFIG['sitelibdir']
|
7
|
+
installdir = sitelibdir + '/win32'
|
8
|
+
file = 'lib\win32\shortcut.rb'
|
9
|
+
|
10
|
+
Dir.mkdir(installdir) unless File.exists?(installdir)
|
11
|
+
File.copy(file, installdir, true)
|
@@ -0,0 +1,206 @@
|
|
1
|
+
require 'win32ole'
|
2
|
+
|
3
|
+
module Win32
|
4
|
+
class Shortcut
|
5
|
+
VERSION = '0.2.0'
|
6
|
+
|
7
|
+
# Activates and displays a window. If the window is minimized or maximized,
|
8
|
+
# the system restores it to its original size and position. An application
|
9
|
+
# should specify this flag when displaying the window for the first time.
|
10
|
+
#
|
11
|
+
SHOWNORMAL = 1
|
12
|
+
|
13
|
+
# Activates the window and displays it as a maximized window.
|
14
|
+
#
|
15
|
+
SHOWMAXIMIZED = 3
|
16
|
+
|
17
|
+
# Displays the window in its minimized state, leaving the currently active
|
18
|
+
# window as active.
|
19
|
+
#
|
20
|
+
SHOWMINNOACTIVE = 7
|
21
|
+
|
22
|
+
# Creates and returns a Shortcut object. In block form it yields +self+
|
23
|
+
# and automatically ensures that Shortcut#save is called at the end of
|
24
|
+
# the block. In non-block form it does not actually create the shortcut
|
25
|
+
# until the Shorcut#save method is called.
|
26
|
+
#
|
27
|
+
def initialize(file)
|
28
|
+
@file = file
|
29
|
+
@shell = WIN32OLE.new('WScript.Shell')
|
30
|
+
@link = @shell.CreateShortcut(file)
|
31
|
+
|
32
|
+
if block_given?
|
33
|
+
begin
|
34
|
+
yield self
|
35
|
+
ensure
|
36
|
+
save
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Identical to Shortcut#new except that it will raise an ArgumentError
|
42
|
+
# unless the +file+ already exists.
|
43
|
+
#
|
44
|
+
def self.open(file)
|
45
|
+
raise ArgumentError, 'shortcut not found' unless File.exists?(file)
|
46
|
+
self.new(file)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns any arguments (i.e. command line options) for the shortcut.
|
50
|
+
#
|
51
|
+
def arguments
|
52
|
+
@link.Arguments
|
53
|
+
end
|
54
|
+
|
55
|
+
# Sets the arguments (i.e. command line options) for the shortcut.
|
56
|
+
#
|
57
|
+
def arguments=(args)
|
58
|
+
@link.Arguments = args
|
59
|
+
end
|
60
|
+
|
61
|
+
# Returns the description (i.e. comment) for the shortcut.
|
62
|
+
#
|
63
|
+
def description
|
64
|
+
@link.Description
|
65
|
+
end
|
66
|
+
|
67
|
+
# Sets the description for the shortcut.
|
68
|
+
#
|
69
|
+
def description=(desc)
|
70
|
+
@link.Description = desc
|
71
|
+
end
|
72
|
+
|
73
|
+
# Returns the file name of the shortcut.
|
74
|
+
#
|
75
|
+
def file
|
76
|
+
@file
|
77
|
+
end
|
78
|
+
|
79
|
+
# Returns the hotkey (i.e. shortcut key) associated to the shortcut, in
|
80
|
+
# the form of a 2-byte number of which the first byte identifies the
|
81
|
+
# modifiers (Ctrl, Alt, Shift) and the second is the ASCII code of
|
82
|
+
# the character key.
|
83
|
+
#
|
84
|
+
def hotkey
|
85
|
+
@link.HotKey
|
86
|
+
end
|
87
|
+
|
88
|
+
# Sets the hotkey for the shortcut.
|
89
|
+
#
|
90
|
+
def hotkey=(key)
|
91
|
+
@link.HotKey = key
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns the name of the file that contain the icon for the shortcut.
|
95
|
+
# In practice this is almost always blank. YMMV.
|
96
|
+
#
|
97
|
+
def icon_location
|
98
|
+
@link.IconLocation
|
99
|
+
end
|
100
|
+
|
101
|
+
# Sets the name of the icon file to be used for the shortcut.
|
102
|
+
#
|
103
|
+
def icon_location=(location)
|
104
|
+
@link.IconLocation = location
|
105
|
+
end
|
106
|
+
|
107
|
+
# Returns the target of the shortcut. This is, joined with arguments, the
|
108
|
+
# content of the "Target" field in a Shortcut Properties Dialog Box. The
|
109
|
+
# target name is returned in 8.3 format.
|
110
|
+
#
|
111
|
+
def path
|
112
|
+
@link.TargetPath
|
113
|
+
end
|
114
|
+
|
115
|
+
alias target_path path
|
116
|
+
|
117
|
+
# Sets the target of the shortcut.
|
118
|
+
#
|
119
|
+
def path=(link_path)
|
120
|
+
@link.TargetPath = link_path
|
121
|
+
end
|
122
|
+
|
123
|
+
alias target_path= path=
|
124
|
+
|
125
|
+
# Attempts to automatically resolve a shortcut and returns the resolved path,
|
126
|
+
# or raises an error. In case no resolution was made, the path is returned
|
127
|
+
# unchanged.
|
128
|
+
#
|
129
|
+
# Note that the path is automatically updated in the path attribute of the
|
130
|
+
# Shortcut object.
|
131
|
+
#
|
132
|
+
def resolve
|
133
|
+
@link.FullName
|
134
|
+
end
|
135
|
+
|
136
|
+
# Returns the type of window style used by a shortcut. The possible
|
137
|
+
# return values are 'normal', 'maximized', or 'minimized'.
|
138
|
+
#
|
139
|
+
def window_style
|
140
|
+
case @link.WindowStyle
|
141
|
+
when SHOWNORMAL
|
142
|
+
'normal'
|
143
|
+
when SHOWMAXIMIZED
|
144
|
+
'maximized'
|
145
|
+
when SHOWMINNOACTIVE
|
146
|
+
'minimized'
|
147
|
+
else
|
148
|
+
'unknown' # Should never reach here
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Deprecated.
|
153
|
+
alias show_cmd window_style
|
154
|
+
|
155
|
+
# Sets the window style to a shortcut. The +style+ can be one of the
|
156
|
+
# following three constants or equivalent string values:
|
157
|
+
#
|
158
|
+
# * SHOWNORMAL or 'normal'
|
159
|
+
# * SHOWMAXIMIZED or 'maximized'
|
160
|
+
# * SHOWMINNOACTIVE or 'minimized'
|
161
|
+
#
|
162
|
+
# Please see the documentation for those constants for further details.
|
163
|
+
#
|
164
|
+
def window_style=(style)
|
165
|
+
valid = [SHOWNORMAL, SHOWMAXIMIZED, SHOWMINNOACTIVE]
|
166
|
+
valid.concat(['normal', 'maximized', 'minimized'])
|
167
|
+
|
168
|
+
unless valid.include?(style)
|
169
|
+
raise ArgumentError, 'invalid style'
|
170
|
+
end
|
171
|
+
|
172
|
+
if style.is_a?(String)
|
173
|
+
case style.downcase
|
174
|
+
when 'normal'
|
175
|
+
style = SHOWNORMAL
|
176
|
+
when 'maximized'
|
177
|
+
style = SHOWMAXIMIZED
|
178
|
+
when 'minimized'
|
179
|
+
style = SHOWMINNOACTIVE
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
@link.WindowStyle = style
|
184
|
+
end
|
185
|
+
|
186
|
+
# Deprecated.
|
187
|
+
alias show_cmd= window_style=
|
188
|
+
|
189
|
+
# Returns directory in which the targeted program will be executed.
|
190
|
+
# Correspond to the "Start in" field of a Shortcut Properties Dialog Box.
|
191
|
+
#
|
192
|
+
def working_directory
|
193
|
+
@link.WorkingDirectory
|
194
|
+
end
|
195
|
+
|
196
|
+
# Sets the directory in which the targeted program will be executed.
|
197
|
+
#
|
198
|
+
def working_directory=(directory)
|
199
|
+
@link.WorkingDirectory = directory
|
200
|
+
end
|
201
|
+
|
202
|
+
def save
|
203
|
+
@link.Save
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
data/test/tc_shortcut.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
##############################################################################
|
2
|
+
# tc_shortcut.rb
|
3
|
+
#
|
4
|
+
# Test suite for the win32-shortcut package. This will temporarily create
|
5
|
+
# a link to Notepad on C:\, which should be automatically deleted.
|
6
|
+
##############################################################################
|
7
|
+
Dir.chdir('..') if File.basename(Dir.pwd) == 'test'
|
8
|
+
$LOAD_PATH.unshift(Dir.pwd + '/lib')
|
9
|
+
|
10
|
+
require 'win32/shortcut'
|
11
|
+
require 'test/unit'
|
12
|
+
include Win32
|
13
|
+
|
14
|
+
class TC_Shortcut < Test::Unit::TestCase
|
15
|
+
def setup
|
16
|
+
@link = Dir.pwd + '/test.lnk'
|
17
|
+
@s = Shortcut.new(@link)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_version
|
21
|
+
assert_equal('0.2.0', Shortcut::VERSION)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_arguments
|
25
|
+
assert_respond_to(@s, :arguments)
|
26
|
+
assert_respond_to(@s, :arguments=)
|
27
|
+
assert_nothing_raised{ @s.arguments }
|
28
|
+
assert_nothing_raised{ @s.arguments = '-v' }
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_description
|
32
|
+
assert_respond_to(@s, :description)
|
33
|
+
assert_respond_to(@s, :description=)
|
34
|
+
assert_nothing_raised{ @s.description }
|
35
|
+
assert_nothing_raised{ @s.description = "test link" }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_file
|
39
|
+
assert_respond_to(@s, :file)
|
40
|
+
assert_nothing_raised{ @s.file }
|
41
|
+
end
|
42
|
+
|
43
|
+
# TODO: Figure out why hotkey assignment fails here. It works fine inthe
|
44
|
+
# example program.
|
45
|
+
#
|
46
|
+
def test_hotkey
|
47
|
+
assert_respond_to(@s, :hotkey)
|
48
|
+
assert_respond_to(@s, :hotkey=)
|
49
|
+
assert_nothing_raised{ @s.hotkey }
|
50
|
+
#assert_nothing_raised{ @s.hotkey = "CTRL-SHIFT-F" }
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_icon_location
|
54
|
+
assert_respond_to(@s, :icon_location)
|
55
|
+
assert_respond_to(@s, :icon_location=)
|
56
|
+
assert_nothing_raised{ @s.icon_location }
|
57
|
+
assert_nothing_raised{ @s.icon_location = "notepad.exe"}
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_path
|
61
|
+
assert_respond_to(@s, :path)
|
62
|
+
assert_respond_to(@s, :path=)
|
63
|
+
assert_nothing_raised{ @s.path }
|
64
|
+
assert_nothing_raised{ @s.path = 'c:\winnt\notepad.exe' }
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_open
|
68
|
+
shortcut = Shortcut.new(@link)
|
69
|
+
shortcut.save
|
70
|
+
|
71
|
+
assert_respond_to(Shortcut, :open)
|
72
|
+
assert_nothing_raised{ Shortcut.open(@link) }
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_resolve
|
76
|
+
assert_respond_to(@s, :resolve)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_save
|
80
|
+
assert_respond_to(@s, :save)
|
81
|
+
assert_nothing_raised{ @s.save }
|
82
|
+
assert_equal(true, File.exists?(@link))
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_window_style
|
86
|
+
assert_respond_to(@s, :window_style)
|
87
|
+
assert_respond_to(@s, :window_style=)
|
88
|
+
assert_nothing_raised{ @s.window_style }
|
89
|
+
assert_nothing_raised{ @s.window_style = Shortcut::SHOWNORMAL }
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_working_directory
|
93
|
+
assert_respond_to(@s, :working_directory)
|
94
|
+
assert_respond_to(@s, :working_directory=)
|
95
|
+
assert_nothing_raised{ @s.working_directory }
|
96
|
+
assert_nothing_raised{ @s.working_directory = "c:\\" }
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_constants
|
100
|
+
assert_not_nil(Shortcut::SHOWNORMAL)
|
101
|
+
assert_not_nil(Shortcut::SHOWMINNOACTIVE)
|
102
|
+
assert_not_nil(Shortcut::SHOWMAXIMIZED)
|
103
|
+
end
|
104
|
+
|
105
|
+
def teardown
|
106
|
+
@s = nil
|
107
|
+
File.delete(@link) if File.exists?(@link)
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
|
3
|
+
spec = Gem::Specification.new do |gem|
|
4
|
+
gem.name = "win32-shortcut"
|
5
|
+
gem.version = "0.2.0"
|
6
|
+
gem.author = "Daniel J. Berger"
|
7
|
+
gem.email = "djberg96@gmail.com"
|
8
|
+
gem.homepage = "http://www.rubyforge.org/projects/win32utils"
|
9
|
+
gem.platform = Gem::Platform::RUBY
|
10
|
+
gem.summary = "An interface for creating or modifying Windows shortcuts."
|
11
|
+
gem.description = "An interface for creating or modifying Windows shortcuts."
|
12
|
+
gem.test_file = "test/tc_shortcut.rb"
|
13
|
+
gem.has_rdoc = true
|
14
|
+
gem.files = Dir["lib/win32/*.rb"] + Dir["test/*"] + Dir["[A-Z]*"]
|
15
|
+
gem.files.reject! { |fn| fn.include? "CVS" }
|
16
|
+
gem.require_path = "lib"
|
17
|
+
gem.extra_rdoc_files = ["README", "CHANGES"]
|
18
|
+
end
|
19
|
+
|
20
|
+
if $0 == __FILE__
|
21
|
+
Gem.manage_gems
|
22
|
+
Gem::Builder.new(spec).build
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: win32-shortcut
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.2.0
|
7
|
+
date: 2006-10-30 00:00:00 -07:00
|
8
|
+
summary: An interface for creating or modifying Windows shortcuts.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: djberg96@gmail.com
|
12
|
+
homepage: http://www.rubyforge.org/projects/win32utils
|
13
|
+
rubyforge_project:
|
14
|
+
description: An interface for creating or modifying Windows shortcuts.
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Daniel J. Berger
|
31
|
+
files:
|
32
|
+
- lib/win32/shortcut.rb
|
33
|
+
- test/tc_shortcut.rb
|
34
|
+
- CHANGES
|
35
|
+
- examples
|
36
|
+
- install.rb
|
37
|
+
- lib
|
38
|
+
- MANIFEST
|
39
|
+
- README
|
40
|
+
- test
|
41
|
+
- win32-shortcut.gemspec
|
42
|
+
test_files:
|
43
|
+
- test/tc_shortcut.rb
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
extra_rdoc_files:
|
47
|
+
- README
|
48
|
+
- CHANGES
|
49
|
+
executables: []
|
50
|
+
|
51
|
+
extensions: []
|
52
|
+
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
dependencies: []
|
56
|
+
|