Open a file with a .hta file [duplicate] - windows

This question already has answers here:
Open a file with an HTA application
(2 answers)
Closed 2 years ago.
I'm making two HTA-applications. One is to install the other one. The code below is the VBScript in the installer HTA which does so that the computer recognises the .sjs extension (an extension which i've created and which has to do with the HTA being installed).
Public Sub Association(EXT, FileType, FileName, Icon)
Set b = CreateObject("wscript.shell")
b.regwrite "HKCR\" & EXT & "\", FileType
b.regwrite "HKCR\" & FileType & "\", "MY file"
b.regwrite "HKCR\" & FileType & "\DefaultIcon\", Icon
b.regwrite "HKCR\" & FileType & "\shell\open\command\", FileName & " %L"
b.regdelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & EXT & "\Application"
b.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & EXT & "\Application", FileName
b.regdelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & EXT & "\OpenWithList\"
b.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & EXT & "\OpenWithList\a", FileName
End Sub
Association ".sjs", "SJS file", "C:\Users\Donald\my_app.hta","C:\Users\Donald\Desktop\my_icon.ico"
I would like it to do so that when I open a .sjs file, it opens the file C:\Users\Donald\my_app.hta, but like this it opens a dialog box where it says "C:\Users\Donald\Documents\file.sjs isn't a valid Win32 application". How can I do so that it does as I want?

Using On Error Resume Next without error check is irresponsible.
[Dim ] xxx As Object isn't valid VBScript. (Should be: [Dim ] xxx As yyy to emphasize that all typed declarations are illegal in VBScript)
Fix that and try again. Use ftype and assoc to check the result of your registry manipulation.
On second thought:
I think that a .HTA file should/must be opened with mshta.exe. (Should have an "explicitly" somewhere; see below)
Update:
I use isql.hta to work with ADO Databases interactively. Parameters and statements are stored in .isql text files. So to tried to mimic your problem with: "I want .isql files associated with the isql.hta application; proof of success: doubleclick on .isql file opens isql.hta". So
assoc .isql=ISQLFile
.isql=ISQLFile
ftype ISQLFile="X:\pathto\isql.hta" %*
ISQLFile="X:\pathto\isql.hta" %*
Doubleclick =>
---------------------------
M:\lib\amfvbs0703\amsinc.isql
---------------------------
M:\lib\amfvbs0703\amsinc.isql is not a valid Win32 application.
---------------------------
OK
---------------------------
M: is a mapped drive; so Windows thinks it's enemy country.
ftype ISQLFile=c:\WINDOWS\system32\mshta.exe "X:\pathto\isql.hta" %*
ISQLFile=c:\WINDOWS\system32\mshta.exe "X:\pathto\isql.hta" %*
Doubleclick => SUCCESS

Related

Applescript works when output is set to desktop, need output set to Team Drive Folder

I have an Applescript to convert a XLSX to XLS. I can get the file from a Google Team Drive (via Drive File Stream), convert and save it to my desktop. When I try to save it to a "Out" folder on a Google Team Drive I get a error.
I believe my output path isn't correct. I've tried different ways to reach the path, with no luck.
Working code saving to Desktop:
set {year:y, month:m, day:d, time:t} to (current date)
set today to (m as number) & d & y
set TSCfilein to "Volumes:GoogleDrive:Team Drives:Sales:Customer Service:Inventory Feeds:TSC:In:TSC_Master.xlsx"
set TSCfileout to (path to desktop as text) & "TSC" & today & ".xls" as string
tell application "Microsoft Excel"
activate
open file TSCfilein
tell workbook 1
tell sheet 1
save active workbook in TSCfileout as Excel98to2004 file format
end tell
close without saving
end tell
end tell
Changing set TSCfileout to the following causes the error:
set TSCfileout to "Volumes:Google Drive:⁨Team Drives⁩:Sales:Customer Service⁩:Inventory Feeds:TSC:Out" & "TSC" & today & ".xls" as string
The expected result is to save the file Google Team Drive folder.
Error:
"Microsoft Excel got an error: active workbook of sheet 1 of workbook 1 doesn’t understand the “save” message." when I try to point the output file location.
There is a general misunderstanding: HFS paths start always with a disk name, but never with Volumes.
Don't mix it up with POSIX paths.
The startup volume "Macintosh HD" as POSIX path is / as HFS path Macintosh HD
The external volume "Google Drive" as POSIX path is /Volumes/Google Drive as HFS path Google Drive
And there is a missing colon after Out
Your path to the external volume is
set TSCfileout to "Google Drive:⁨Team Drives⁩:Sales:Customer Service⁩:Inventory Feeds:TSC:Out:" & "TSC" & today & ".xls"
The trailing coercion as string is redundant. According to the rules the leftmost type is the result type of a concatenation.

.VBS called by .BAT to create .zip - run silently (without interface)?

I have a .bat file which I use to back up files, which calls a .vbs and passes it two parameters, as follows:
...
ZipCMD.vbs "C:\Source" "C:\Destination\Data.zip"
...
ZipCMD.vbs contains the following code (Credit to garbb):
Set objArgs = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
InputFolder = FS.GetAbsolutePathName(objArgs(0))
ZipFile = FS.GetAbsolutePathName(objArgs(1))
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
numfolderitems = objShell.NameSpace(InputFolder).Items.count
objShell.NameSpace(ZipFile).CopyHere(source)
' wait until number of items in zip file is the same as in the folder we are zipping up
' also sometimes gets errors when getting folder object for zip file, probably because it is in use? so ignore these
On Error Resume Next
Do while True
numitemsinzip = objShell.NameSpace(ZipFile).Items.count
If Err.Number = 0 and numitemsinzip = numfolderitems Then
Exit Do
ElseIf Err.Number <> 0 then
Err.Clear
End If
wScript.Sleep 10
Loop
On Error Goto 0
When the zipping is occurring, the usual windows 'Compressing files' interface appears, and shows the progress bar ticking along for a few minutes, before closing and disappearing.
Question: Can vbs run a compression silently (i.e. without interface)? -- I've read this article, which shows a flag, however this doesn't appear to work with copying to .zip, for some reason.
Follow-up question: If it's not possible for the .vbs which I'm using to achieve this, then is there an alternative way, which still utilises calling another file/process(?) (.vbs / .js, or other?) and feeding it the two paths from cmd?
Edit: I'm trying to achieve this without the use of third-party software (e.g. 7zip), and simply using native windows code.
Suppose I am almost 3 months late on this, but if you have powershell version 5 or later, you can simply create a powershell script:
Compress-Archive "C:\Source" "C:\Destination\Data.zip"
or from a batch file:
powershell Compress-Archive "C:\Source" "C:\Destination\Data.zip"
Also see this option

How to store the contents of the clipboard into a variable? [duplicate]

This question already has answers here:
Use clipboard from VBScript
(15 answers)
How to copy/cut a file (not the contents) to the clipboard in Windows on the command line?
(6 answers)
Closed 3 years ago.
Editor's note:
While this question is specifically about copying a file reference to the clipboard, its generic title led to answers about how to copy / get text.
As an Emacs user on Windows who often attaches files in mails, I have been looking for a utility to copy a file (not its contents) to the clipboard, just as windows explorer does on righclick/copy).
I just found this right here on SO which uses System.Windows.Forms.Clipboard` in a small program to do exactly that. But it is in C#, for which I don't have immediate access to a compiler. So I am wondering if this can be done and how.
I saw several references such as this that the clipboard is not accessible in VBScripting, but msdn shows documentation for VB so I am risking the question.
I have never written a VBScript before but I did try a few things before asking, starting with running a copy pasted a "Hello world" and then various combinations of CreateObject etc.
Update: I need to call Clipboard.SetFileDropList, so I do not think I can use ClipboardData as suggested by the answers, it does not have this method.
Update for visitors
The solution I ended up using was to compile the C# itself, I did not know I already had a compiler.
Another update for visitors
https://stackoverflow.com/a/29963268/18573 is what I am now using, quite happily.
You can do it with an html object to retrieve the contents of the clipboard:
' Get clipboard text
Set objHTML = CreateObject("htmlfile")
text = objHTML.ParentWindow.ClipboardData.GetData("text")
EDIT: I use this snippet to put text back on the clipboard, but it needs third party software; a standalone executable 'clip.exe' which can be found on Windows 2003 Server or just on the internet:
' Do something with the text
text = replace(text, "you ", "you and your dog ")
' Put it back to the clipboard
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("clip")
Set oIn = oExec.stdIn
oIn.WriteLine text
oIn.Close
(Yes, it is all a little bit hackerdyhack)
VBScript doesn't support the clipboard. Most hosts that host vbscript, such as Internet Explorer give access through the host. Therefore vbscript running in IE or an HTA can use IE's clipboard support. The scripting hosts do not give clipboard support. You can use a vbs file to start IE through COM automation, navigate to a local page (to bypass security warnings), then use IE's clipboard.
Here's a code snippit (Outp. is a text stream)
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
ie.Navigate2 "C:\Users\David Candy\Desktop\Filter.html"
Do
wscript.sleep 100
Loop until ie.document.readystate = "complete"
txt=ie.document.parentwindow.clipboardData.GetData("TEXT")
ie.quit
If IsNull(txt) = true then
outp.writeline "No text on clipboard"
else
outp.writeline txt
End If
You need this function (is a little modification of this):
'TO CLEAR
ClipBoard("")
'TO SET
ClipBoard("Hello World!")
'TO GET
Result = ClipBoard(Null)
Function ClipBoard(input)
'#description: A quick way to set and get your clipboard.
'#author: Jeremy England (SimplyCoded)
If IsNull(input) Then
ClipBoard = CreateObject("HTMLFile").parentWindow.clipboardData.getData("Text")
If IsNull(ClipBoard) Then ClipBoard = ""
Else
CreateObject("WScript.Shell").Run _
"mshta.exe javascript:eval(""document.parentWindow.clipboardData.setData('text','" _
& Replace(Replace(Replace(input, "'", "\\u0027"), """","\\u0022"),Chr(13),"\\r\\n") & "');window.close()"")", _
0,True
End If
End Function
For the equivalent of a "paste" operation I would run a command-line utility like ClipOut or paste, redirect output to a file and read the file contents.
return = WshShell.Run("cmd /c clipout.exe > output.txt", 0, true)
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("output.txt", 1)
text = file.ReadAll
file.Close
You can get ClipOut here: http://jasonfaulkner.com/ClipOut.aspx
You can get paste here: https://www.c3scripts.com/tutorials/msdos/paste.html
For the equivalent of a "copy" operation I would use the clip command line utility that actually comes with Windows and similar code as above.
About the clip utility: https://blogs.msdn.microsoft.com/oldnewthing/20091110-00/?p=16093

AppleScript choose file dialog box store last folder opened in text file.

This seems like such an easy thing to do that I'm amazed the solution is still eluding me.
I'm trying to store that last folder opened by a "choose file" dialog box. I want to store that folder's location in a text file.
I want to have my "choose file" dialog always open to the last folder used.
I've got a lot of the script working but there is one weird thing that keeps eluding me.
Look at my script...
set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")
set strPathFromConfig to item 1 of Shows as string
set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)
display dialog strPathFromConfig
set strPath to (path to home folder as text) & strPathFromConfig
display dialog strPath
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath
The script reads my config text file which contains one line and only one string of "Documents".
I trim some leading garbage characters and display a dialog with the result of "Documents".
Then I set the strPath using the the value from the config file.
I display the new value is it is a valid location on my system.
Next I attempt to the dialog and I get an error message of "File alias Macintosh HD:Users:lowken:Documents of wasn't found.
Let change the script so that instead of using the value that was extracted from the config.txt file, I simply set a string variable in my script.
set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")
set strPathFromConfig to item 1 of Shows as string
set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)
display dialog strPathFromConfig
set strTemp to "Documents"
set strPath to (path to home folder as text) & strTemp
display dialog strPath
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath
Now it works. AppleScript seems to not want to use the value that was looked up from the config.txt file.
What am I doing wrong? I tried casting to an Alias I tried different locations on my system.
It seems to me that somehow the value looked up from the text file is not a string data type.
Any ideas?
P.S.
I'm running OS X Yosemite 10.10.4 on a mid 2012 MacBook Pro.
The problem is the encoding of your "config.txt" document.
The read command can read (MacRoman, UTF-8 or UTF-16) encoded text files, but you must specify the type class, otherwise it read the text file as MacRoman.
set strPathFromConfig to paragraph 1 of (read "/Users/lowken/Documents/config.txt" as «class ut16») -- or if UTF-8 use —> as «class utf8»
set strPath to ((path to home folder as text) & strPathFromConfig) as alias
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location strPath
For another encoding, you must change the encoding of the "config.txt" document.
The default location of the choose file command can be a Unix path "/Users/my_user/Documents" or a Finder path with Alias before like : alias "HD:Users:my_User:Documents"
so first check that strPath is the correct value, then if OK, double check the class of it :
Display Dialog (class of strPath) as string
it may not be OK and you have to coerce the (path to home folder as string) and not text.

How can I use Clipboard in vbscript? [duplicate]

This question already has answers here:
Use clipboard from VBScript
(15 answers)
How to copy/cut a file (not the contents) to the clipboard in Windows on the command line?
(6 answers)
Closed 3 years ago.
Editor's note:
While this question is specifically about copying a file reference to the clipboard, its generic title led to answers about how to copy / get text.
As an Emacs user on Windows who often attaches files in mails, I have been looking for a utility to copy a file (not its contents) to the clipboard, just as windows explorer does on righclick/copy).
I just found this right here on SO which uses System.Windows.Forms.Clipboard` in a small program to do exactly that. But it is in C#, for which I don't have immediate access to a compiler. So I am wondering if this can be done and how.
I saw several references such as this that the clipboard is not accessible in VBScripting, but msdn shows documentation for VB so I am risking the question.
I have never written a VBScript before but I did try a few things before asking, starting with running a copy pasted a "Hello world" and then various combinations of CreateObject etc.
Update: I need to call Clipboard.SetFileDropList, so I do not think I can use ClipboardData as suggested by the answers, it does not have this method.
Update for visitors
The solution I ended up using was to compile the C# itself, I did not know I already had a compiler.
Another update for visitors
https://stackoverflow.com/a/29963268/18573 is what I am now using, quite happily.
You can do it with an html object to retrieve the contents of the clipboard:
' Get clipboard text
Set objHTML = CreateObject("htmlfile")
text = objHTML.ParentWindow.ClipboardData.GetData("text")
EDIT: I use this snippet to put text back on the clipboard, but it needs third party software; a standalone executable 'clip.exe' which can be found on Windows 2003 Server or just on the internet:
' Do something with the text
text = replace(text, "you ", "you and your dog ")
' Put it back to the clipboard
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("clip")
Set oIn = oExec.stdIn
oIn.WriteLine text
oIn.Close
(Yes, it is all a little bit hackerdyhack)
VBScript doesn't support the clipboard. Most hosts that host vbscript, such as Internet Explorer give access through the host. Therefore vbscript running in IE or an HTA can use IE's clipboard support. The scripting hosts do not give clipboard support. You can use a vbs file to start IE through COM automation, navigate to a local page (to bypass security warnings), then use IE's clipboard.
Here's a code snippit (Outp. is a text stream)
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
ie.Navigate2 "C:\Users\David Candy\Desktop\Filter.html"
Do
wscript.sleep 100
Loop until ie.document.readystate = "complete"
txt=ie.document.parentwindow.clipboardData.GetData("TEXT")
ie.quit
If IsNull(txt) = true then
outp.writeline "No text on clipboard"
else
outp.writeline txt
End If
You need this function (is a little modification of this):
'TO CLEAR
ClipBoard("")
'TO SET
ClipBoard("Hello World!")
'TO GET
Result = ClipBoard(Null)
Function ClipBoard(input)
'#description: A quick way to set and get your clipboard.
'#author: Jeremy England (SimplyCoded)
If IsNull(input) Then
ClipBoard = CreateObject("HTMLFile").parentWindow.clipboardData.getData("Text")
If IsNull(ClipBoard) Then ClipBoard = ""
Else
CreateObject("WScript.Shell").Run _
"mshta.exe javascript:eval(""document.parentWindow.clipboardData.setData('text','" _
& Replace(Replace(Replace(input, "'", "\\u0027"), """","\\u0022"),Chr(13),"\\r\\n") & "');window.close()"")", _
0,True
End If
End Function
For the equivalent of a "paste" operation I would run a command-line utility like ClipOut or paste, redirect output to a file and read the file contents.
return = WshShell.Run("cmd /c clipout.exe > output.txt", 0, true)
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("output.txt", 1)
text = file.ReadAll
file.Close
You can get ClipOut here: http://jasonfaulkner.com/ClipOut.aspx
You can get paste here: https://www.c3scripts.com/tutorials/msdos/paste.html
For the equivalent of a "copy" operation I would use the clip command line utility that actually comes with Windows and similar code as above.
About the clip utility: https://blogs.msdn.microsoft.com/oldnewthing/20091110-00/?p=16093

Resources