Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/78ecd911864cb9625204 to your computer and use it in GitHub Desktop.
Save atifaziz/78ecd911864cb9625204 to your computer and use it in GitHub Desktop.
Windows script to list shell verbs or execute one for a given file
fso = new ActiveXObject('Scripting.FileSystemObject')
script = do (path = WScript.ScriptFullName) ->
path : path
fname: fso.GetFileName(path)
about = """
Explorer Shell Do 1.0
Copyright (C) 2014 Atif Aziz. All rights reserved.
USAGE:
#{script.fname} PATH [VERB]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License."""
[stdin, stdout, stderr] = [WScript.StdIn, WScript.StdOut, WScript.StdErr]
writeln = (s, w = stdout) -> w.WriteLine(s)
write = (s, w = stdout) -> w.Write(s)
echo = (s) -> WScript.Echo(s)
alert = echo
exit = (code) -> if typeof code is 'number' then WScript.Quit(code) else WScript.Quit()
main = (args) ->
if args.isFlagged('?') or args.isFlagged('h') or args.isFlagged('help')
writeln about
return
verbose = args.isFlagged('v')
dryRun = args.isFlagged('n')
[path, verb] = args.unnamed
if not path then throw 'Missing file path.'
if not fso.FileExists(path)
throw "File not found: #{path}"
path = fso.GetAbsolutePathName(path)
dirPath = fso.GetParentFolderName(path)
filename = fso.GetFileName(path)
shell = new ActiveXObject('Shell.Application')
folder = shell.NameSpace(dirPath)
file = folder.ParseName(filename)
verbs = file.Verbs()
verbs = for i in [0...verbs.Count] when name = verbs.Item(i).Name
name: name.replace('&', '')
'do': do(v = verbs.Item(i)) -> -> v.DoIt(); return
do (verb = verb and verb.toUpperCase()) ->
for v in verbs
if not verb then writeln v.name
else if verb is v.name.toUpperCase()
if verbose then writeln "Doing \"#{v.name}\" on \"#{path}\"", stderr
v.do() unless dryRun
exit()
if verb then throw "Verb \"#{verb}\" not available for \"#{path}\""
do ->
try
wshargs = WScript.Arguments
args = (wshargs.item(i) for i in [0...wshargs.length])
args.unnamed = (wshargs.Unnamed.Item(i) for i in [0...wshargs.Unnamed.Count])
args.getNamed = (name) -> WScript.Arguments.Named.Item(name)
args.isFlagged = (name) -> WScript.Arguments.Named.Exists(name)
main(args)
catch e
writeln (if not e.message then e.toString() else e.message), stderr
exit(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment