Can´t run notepad in v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
NotAColdSun
Posts: 57
Joined: 05 Aug 2022, 01:07

Can´t run notepad in v2

Post by NotAColdSun » 12 Mar 2023, 16:47

I can´t run/open a .txt or .ahk file.

Code: Select all

Run(A_ScriptDir "/Calendar.txt")
I get this message: "Not a valid filename".
I guess the best thing would be to make it to open it in notepad and/or VS code, but I had no luck in converting lines from my v1 ahk.

PS. it seems it´s not a common problem, but please help if you experienced similar. All other types of files (pdf, docx, folders) open perfectly, and .txt and .ahk files open perfectly in v1, same links.
See attachments.

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Can´t run notepad in v2

Post by mikeyww » 12 Mar 2023, 16:59

Code: Select all

#Requires AutoHotkey v2.0
filePath := A_ScriptDir '\Calendar.txt'
If FileExist(filePath)
 Run 'notepad "' filePath '"'
Else MsgBox 'File not found.`n`n' filePath, 'Error', 48

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Can´t run notepad in v2

Post by swagfag » 12 Mar 2023, 17:24

theres no problem with the code as is on my computer. post what the path expands to and an image of the error ure seeing

NotAColdSun
Posts: 57
Joined: 05 Aug 2022, 01:07

Re: Can´t run notepad in v2

Post by NotAColdSun » 13 Mar 2023, 02:46

I tried your code, and get exactly the same error (as the first image from the top).

The full path is:
"C:\Users\ThisUser\Dropbox\U\Calendar.txt"

For some reason, the attachments have been uploaded, and when trying to upload a new one, I get a triangle on the right side, with "Sorry, the board attachment quota has been reached."
These are the images.
https://ibb.co/fvk6hNv
https://ibb.co/WvF4yKH

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Can´t run notepad in v2

Post by swagfag » 13 Mar 2023, 03:56

NotAColdSun wrote:
13 Mar 2023, 02:46
The full path is:
"C:\Users\ThisUser\Dropbox\U\Calendar.txt"
then what is that
Image
a colon : is not a valid windows filename character

NotAColdSun
Posts: 57
Joined: 05 Aug 2022, 01:07

Re: Can´t run notepad in v2

Post by NotAColdSun » 13 Mar 2023, 04:30

swagfag wrote:
13 Mar 2023, 03:56
a colon : is not a valid windows filename character
The colon comes from "copy as path", but I´m aware of not using it in my ahk script.

The path I use in the script is this one (= the path of the script I received from mikeyww):
A_ScriptDir '\Calendar.txt'
The weird thing is, it all works in v1, and in v2 in other files than .txt and .ahk, so I know the path is the right one.

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Can´t run notepad in v2

Post by mikeyww » 13 Mar 2023, 06:52

Attachments? Uploads? How do attachments and uploads relate to your problem description in this thread?

You haven't explained what you are doing. If running my exact script generated an error message about the filename, then something fishy is happening. Are you somehow trying to run the script on a remote server?

What does the following script show, without changing it?

Code: Select all

#Requires AutoHotkey v2.0
filePath := A_ScriptDir '\Calendar.txt'
MsgBox A_Clipboard := A_ScriptDir, 'A_ScriptDir', 64
If FileExist(filePath)
 Run 'notepad "' filePath '"'
Else MsgBox 'File not found.`n`n' filePath, 'Error', 48

NotAColdSun
Posts: 57
Joined: 05 Aug 2022, 01:07

Re: Can´t run notepad in v2

Post by NotAColdSun » 13 Mar 2023, 12:38

By attachments/uploads I mean the screenshots I tried to upload in this thread.

1. The message box shows: C:\Users\ThisUser\Dropbox\U
2. Notepad opens, with a message "filename not valid" (ikke et gyldigt filnavn) (see link to screenshot below)
https://ibb.co/DVvb12f
3. after I click "ok", notepad opens as "uden titel" (untitled), and when I try to save it, it opens a save dialog box in the same destination folder as in 1.

I did not use any remote server, I run in on my own Windows 11 PC.

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Can´t run notepad in v2

Post by mikeyww » 13 Mar 2023, 14:46

1. You could upload (attach) an example of a text file that demonstrates this problem with the posted script.

2. Are you running the script as administrator?

3. If you move the two files to a different directory (e.g., MyDocuments), does it work?

4. Your screenshot does not include the Notepad window itself. Can you add a screenshot that shows both Notepad and the error message together?

NotAColdSun
Posts: 57
Joined: 05 Aug 2022, 01:07

Re: Can´t run notepad in v2

Post by NotAColdSun » 14 Mar 2023, 03:34

1. there is no other text than the one in my previous attachment(s)
2. no. Only v1 is ran as administrator, with this code. I tried to adjust the v1 code below to v2, but it did not work:

Code: Select all

#SingleInstance Force ; run as adm script
SetWorkingDir %A_ScriptDir%
if not A_isAdmin
	Run *RunAs “%A_ScriptFullPath%”
3. no, it doesn´t. I encounter the same issue no matter the location of the file.
4.
a) This is the whole notepad screen that appears:
https://ibb.co/xsF1DPk
b) after clicking "yes" to "would you like to create a new file", I get this message:
https://ibb.co/bKCGgBN
c) and the save dialog box:
https://ibb.co/Nx1YtYV

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Can´t run notepad in v2

Post by mikeyww » 14 Mar 2023, 06:31

Hmm. Nothing in your screenshots looks familiar to me, and I was unable to find any attachments in your posts, so I will let others solve this puzzle!

NotAColdSun
Posts: 57
Joined: 05 Aug 2022, 01:07

Re: Can´t run notepad in v2

Post by NotAColdSun » 14 Mar 2023, 10:04

Well, but since opening other file types works, maybe I could use VS code. My v1 code below works perfectly, but I was unable to convert it to a functioning v2 code. Any suggestion?

Code: Select all

^8::
IfWinNotExist, Calendar
Run, "C:\Users\%A_UserName%\AppData\Local\Programs\Microsoft VS Code\Code.exe" %A_ScriptDir%\lib\Calendar.txt, UTF-8
WinActivate, Calendar
return

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Can´t run notepad in v2

Post by mikeyww » 14 Mar 2023, 10:38

The working directory is UTF-8?

NotAColdSun
Posts: 57
Joined: 05 Aug 2022, 01:07

Re: Can´t run notepad in v2

Post by NotAColdSun » 14 Mar 2023, 17:45

no, the working directory is the script directory. UTF-8 should be an option, but is not necessary.

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Can´t run notepad in v2

Post by gregster » 14 Mar 2023, 18:48

If you put UTF-8 into the second parameter of Run, it sets the working directory of the program you run to a directory named UTF-8 - which might be part of your problem. This is what mikeyww alludes to.

Options would be the third parameter of Run - but anyway it doesn't have an UTF-8 option according to the docs .

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Can´t run notepad in v2

Post by boiler » 14 Mar 2023, 18:49

The point is that UTF-8 would only make sense where you put it if that happened to be the name of the working directory because you put it in the WorkingDir parameter. Even if you put it in the correct position of the Options parameter, UTF-8 is not a valid option for this command/function. You can't make up syntax. Review the Run (for v1) and/or Run (for v2) documentation to see a detailed description of its parameters.

You should either replace UTF-8 with the actual working directory or leave it out. If it happens to work with UTF-8 there, it's because the application is ignoring the working directory being passed to it.

Edit: Too slow! :)

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Can´t run notepad in v2

Post by mikeyww » 14 Mar 2023, 21:16

More ideas:

Code: Select all

#Requires AutoHotkey v2.0
vscode   := EnvGet('LOCALAPPDATA') "\Programs\Microsoft VS Code\Code.exe"
winTitle := 'ahk_exe' vscode
cal      := A_ScriptDir '\lib\Calendar.txt'

^8:: {
 If !WinExist(winTitle)
  Run(vscode ' ' cal), WinWait(winTitle,, 3)
 WinActivate
}

NotAColdSun
Posts: 57
Joined: 05 Aug 2022, 01:07

Re: Can´t run notepad in v2

Post by NotAColdSun » 15 Mar 2023, 02:02

Thank you for explaining UTF-8, but this is not the problem, since I did not include UTF-8 in my v2 script.

The script with opening a .txt file in VS code works well, thank you!
But: I cannot find any info about 'LOCALAPPDATA': is it a built-in variable, since I have not got any error message that the variable was not defined?
Maybe I can try to use the same trick to open in Notepad?

Also: if you have the lines of code to run v2 script as administrator, please, share them. I only have it for v1.

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Can´t run notepad in v2

Post by boiler » 15 Mar 2023, 04:30

NotAColdSun wrote: But: I cannot find any info about 'LOCALAPPDATA': is it a built-in variable, since I have not got any error message that the variable was not defined?
It is a Windows environment variable, thus the use of EnvGet.

NotAColdSun wrote: Maybe I can try to use the same trick to open in Notepad?
You could try using the environment variable TEMP and put your file in C:\Users\{username}\AppData\Local\Temp


Post Reply

Return to “Ask for Help (v2)”