Sign in to follow this  
Followers 0
erix

Open a file with a specified application

13 posts in this topic

Hello

How can i open a file with a specified exe.

For example i'd like to have a button who open config.txt with notepad and an other button who open config.txt with word (for example)

I try with shellexecute but i don't find where to put the exe file and the txt file in the command

Thanks

Eric

Share this post


Link to post
Share on other sites

#2 ·  Posted (edited)

Hello

How can i open a file with a specified exe.

For example i'd like to have a button who open config.txt with notepad and an other button who open config.txt with word (for example)

I try with shellexecute but i don't find where to put the exe file and the txt file in the command

Thanks

Eric

Edit accordingly based upon the path to your Word executable:

CODE
#include <GUIConstants.au3>

$file = "C:\Config.txt"

GUICreate("Open With...",400,200)

$buttonNotepad = GUICtrlCreateButton("Open with Notepad",10,10,150,20)

$buttonWord = GUICtrlCreateButton("Open with Word",10,40,150,20)

GUISetState (@SW_SHOW)

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

Exit

Case $msg = $buttonNotepad

Run("C:\WINDOWS\NOTEPAD.EXE " &$file)

Case $msg = $buttonWord

Run("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE " &$file) ; edit path based upon your version of Word

EndSelect

Wend

Edit: grammar

Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites

Thank you Monamo

It works

But if for example Word is not on the same path on different machine.

I have an other button for choose the path of the WinWord.exe

The path is saved after.

In this case the path is not like this :

Run("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE " &$file)

But like this

Run($path & $file)

But Autoit doesn't accept Run($path & $file)

Thanks

Share this post


Link to post
Share on other sites

Thank you Monamo

It works

But if for example Word is not on the same path on different machine.

I have an other button for choose the path of the WinWord.exe

The path is saved after.

In this case the path is not like this :

Run("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE " &$file)

But like this

Run($path & $file)

But Autoit doesn't accept Run($path & $file)

Thanks

Try Run ("" & $path & $file)

Share this post


Link to post
Share on other sites

How about:

$sFile = "C:\Temp\DingleBerries.doc"
ShellExecute($sFile, "", "", "Open")

If the file type has an application association already, that should be all you need to get it opened.

<_<


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Try Run ("" & $path & $file)

Not quite <_<...

Run('"' & $sPathToExe & '" "' & $sFileToRun & '"')


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Try Run ("" & $path & $file)

Thanks but it doesn't work

If the file type has an application association already, that should be all you need to get it opened.

Yes of course but here i want to choose the application.

Run('"' & $sPathToExe & '" "' & $sFileToRun & '"')

Thanks too but it doesn't work

Share this post


Link to post
Share on other sites

Run('"' & $sPathToExe & '" "' & $sFileToRun & '"')

This should work as long as $sPathToExe doesn't contain any double quotes around it already.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Thank you Monamo

It works

But if for example Word is not on the same path on different machine.

I have an other button for choose the path of the WinWord.exe

The path is saved after.

In this case the path is not like this :

Run("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE " &$file)

But like this

Run($path & $file)

But Autoit doesn't accept Run($path & $file)

Thanks

OK, here ya go. This variation should open the file in Word no matter which computer you run it on, assuming that SOME version of word is on it (as best I can tell <_<). It queries the registry for the default Word document handler and uses that for the "Word" button.

CODE
#include <GUIConstants.au3>

GUICreate("Open With...",400,200)

$file = "C:\Config.txt"

$sNotepadPath = @WindowsDir &"\notepad.exe"

$buttonNotepad = GUICtrlCreateButton("Open with Notepad",10,10,150,20)

$buttonWord = GUICtrlCreateButton("Open with Word",10,40,150,20)

$sWordPath = _GetWordPath()

GUISetState (@SW_SHOW)

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

Exit

Case $msg = $buttonNotepad

Run($sNotepadPath & " " &$file)

Case $msg = $buttonWord

Run($sWordPath & " " &$file)

EndSelect

Wend

Func _GetWordPath()

$classRoot = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document\CurVer","")

$installedPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Classes\" & $classRoot & "\shell\Open\command","")

$wordPathParsing = StringSplit($installedPath,'"')

For $i= 1 To $wordPathParsing[0]

If FileExists($wordPathParsing[$i]) Then

$installedPath = $wordPathParsing[$i]

ExitLoop

EndIf

Next

If FileExists($installedPath) Then

Return $installedPath

Else

MsgBox(48,"Error:","Unable to determine path to Microsoft Word executable on this system.")

GUICtrlSetState($buttonWord,$GUI_DISABLE)

EndIf

EndFunc

Edit: Code correction to parse out parameters passed at the end of the registry entry for word path Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

That's works with notepad.exe but with winword.exe i have an error message after the launch who said :

(Sorry it's a french-english translation.)

Impossible to open the document

Try this operation

* In the file menu, click open for open the document

* Verify than the document have a .doc extension

(c:\Documents)

Of course in the example i have a txt file but i try with a doc file too.

Thanks

Edited by erix

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

That's works with notepad.exe but with winword.exe i have an error message after the launch who said :

(Sorry it's a french-english translation.)

Impossible to open the document

Try this operation

* In the file menu, click open for open the document

* Verify than the document have a .doc extension

(c:\Documents)

Of course in the example i have a txt file but i try with a doc file too.

Thanks

Two things:
  • I posted a correction to my code in the post above to handle extra parameters stored in the "command" path in the registry for opening Word docs
  • The error you mention sounds like an issue with spaces in the file path. Try the code below when defining your text file path (insert the "real" path accordingly). Rather than deal with all the confusion resulting from trying to make the quotation marks (") part of the path, this will force a set of quotes at the beginning and end of the path, hopefully taking care of your problem.
$file = Chr(34) &"C:\Documents and Settings\User\Desktop\config.txt" &Chr(34)
Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites

This opens a PowerPoint presentation in OpenOffice.org 2.2, even if the file type is registered to MS PowerPoint, and regardless of where OOo was installed:

; Read path from the registry
$sOOoReg = "HKEY_CLASSES_ROOT\Applications\OpenOffice.org 2.2\shell\edit\command"
$sOOoOpen = RegRead($sOOoReg, "")
; Trim out just the path
$sOOoOpen = StringReplace(StringLeft($sOOoOpen, StringInStr($sOOoOpen, "-o") - 1), '"', '')
$sFile = '"' & @ScriptDir & '\Test.ppt"'
; Run it
ShellExecute($sOOoOpen, $sFile, @WorkingDir, "Open")

<_<


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

This opens a PowerPoint presentation in OpenOffice.org 2.2, even if the file type is registered to MS PowerPoint, and regardless of where OOo was installed:

; Read path from the registry
$sOOoReg = "HKEY_CLASSES_ROOT\Applications\OpenOffice.org 2.2\shell\edit\command"
$sOOoOpen = RegRead($sOOoReg, "")
; Trim out just the path
$sOOoOpen = StringReplace(StringLeft($sOOoOpen, StringInStr($sOOoOpen, "-o") - 1), '"', '')
$sFile = '"' & @ScriptDir & '\Test.ppt"'
; Run it
ShellExecute($sOOoOpen, $sFile, @WorkingDir, "Open")

:P

Thank you PsaltyDS and Monamo for this code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0