Search for a file and open it Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JuaninBeats
Posts: 21
Joined: 22 Jan 2022, 20:20

Search for a file and open it

Post by JuaninBeats » 22 Jan 2022, 20:30

Hi!

I'm musician. I'm very begginer using autohotkey.

I'm trying to create a script that when I select a text (a song's name), pressing a combination of keys, the script search it on my Ableton Project Folder (and subfolders) and open the project with that name in it.

I create this and it's working amazing:

Code: Select all

^A::
	clip := clipboard
	send, ^c
	url := "C:\Ableton Projects\"
	joined_url = %url%%clipboard% project\%clipboard%.als
	run, %joined_url%
return
For example if I press Ctrl+A selecting the text "Valerie (Amy Winehouse)", it goes to the folder "C:\Ableton Projects\Valerie (Amy Winehouse) Project\Valerie (Amy Winehouse).als" and opens it. Works amazing.
But in that way I have to rename 100 ableton projects. I would love that selecting the text "Valerie" and pressing Ctrl+A, the script could search on the "C:\Ableton Projects\" folder for any .als file that contains "Valerie", and execute it...
Or best yet: select the text "Valerie (Amy Winehouse)", then press Ctrl+A, and the script could search on that folder and opens the file named "Amy Winehouse - Valerie.als". That would be the best option.

I would appreciate very much your help. :)
Thank you very much!

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

Re: Search for a file and open it

Post by mikeyww » 22 Jan 2022, 20:58

Code: Select all

base = C:\Ableton Projects
ext  = als

^a::
Send % ("^c", Clipboard := "")
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else Loop, Files, %base%\*.%ext%, R
 If A_LoopFileName contains %Clipboard%
  Run, %A_LoopFilePath%
Return

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Search for a file and open it

Post by JoeWinograd » 22 Jan 2022, 22:02

Hi Juan,

I see that this is your first post here, so let me start with...Welcome Aboard!
JuaninBeats wrote:I would love that selecting the text "Valerie" and pressing Ctrl+A, the script could search on the "C:\Ableton Projects\" folder for any .als file that contains "Valerie", and execute it
Here's a script for that:

Code: Select all

RootFolder:="C:\Ableton Projects" ; root folder for all projects (without the ending backslash)
FileExt:="als" ; file extension for proejcts (without the dot)
^a:: ; Ctrl+a - imo, this is a bad choice for the hotkey, as it is the Windows standard for Select All - I suggest using a different hotkey
Clipboard:="" ; clear clipboard
SendInput,^c ; copy selected text to clipboard
ClipWait,0.1 ; wait 100ms for text to arrive on clipboard
If (ErrorLevel=1)
{
  MsgBox,4144,Error,Text did not appear on clipboard after 100ms
  Return
}
SearchTerm:=Clipboard
Loop,Files,%RootFolder%\*.%FileExt%
{
  If (InStr(A_LoopFileName,SearchTerm)) ; look for project name
  {
    ; found it
    Run,%A_LoopFilePath% ; run it
    Return
  }
}
MsgBox,4144,Error - Not Found,Did not find a project with this in it:`n`n%SearchTerm%
Return
Or best yet: select the text "Valerie (Amy Winehouse)", then press Ctrl+A, and the script could search on that folder and opens the file named "Amy Winehouse - Valerie.als".
That one is trickier, because the text Valerie (Amy Winehouse) has to be converted to Amy Winehouse - Valerie to find the file. Also, is the text always in the format of Valerie (Amy Winehouse) or might there be other formats, such as Valerie [Amy Winehouse], Valerie - Amy Winehouse, (Amy Winehouse) - Valerie, Amy Winehouse - Valerie, etc.? In other words, are there other variants of the text Valerie (Amy Winehouse) that need to be recognized?

Regards, Joe

JuaninBeats
Posts: 21
Joined: 22 Jan 2022, 20:20

Re: Search for a file and open it

Post by JuaninBeats » 23 Jan 2022, 12:13

Wow thank you very much for your fast answers!
You make it seem so easy, but I'm still processing the scripts :crazy: haha.

I test both of scripts.

Don't know how to quote comments I'm sorry.

@mikeyww your code works very well! It does the job. I was missing a error message when the file wasn't found. And there you go @JoeWinograd (thank you very much for the warm welcoming :) ). Your script at first didn't work, but then I just figured it out that an "r" was missing here:

Code: Select all

Loop,Files,%RootFolder%\*.%FileExt%, R
And boom! It worked! :D

Answering your question @JoeWinograd , yes, it would be always the same text structure: Valerie (Amy Winehouse) --> Song's name (Artist)
I would really love to improve this script to achieve this last best option.

Thank you very much!

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

Re: Search for a file and open it

Post by mikeyww » 23 Jan 2022, 12:38

More ideas:

Code: Select all

base = C:\Ableton Projects
ext  = als

^a::
Send % ("^c", Clipboard := "")
ClipWait, 0
If !ErrorLevel {
 RegExMatch(Clipboard, "(.+)\((.+)\)", m), fn := Trim(m2) " - " Trim(m1) "." ext
 Loop, Files, %base%\%fn%, R
  Run, %A_LoopFilePath%
} Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Search for a file and open it

Post by JoeWinograd » 23 Jan 2022, 14:07

Hi Juan,
JuaninBeats wrote:I just figured it out that an "r" was missing
Good job finding the bug in my code! Yes, the R mode (for Recursion into subfolders) was missing. I tested here before posting, but only on a root level folder...no subfolders...my bad.
yes, it would be always the same text structure: Valerie (Amy Winehouse) --> Song's name (Artist)
That makes it easier and I see that mikey has already posted a RegExMatch for it, so I won't bother updating my code.
Don't know how to quote comments I'm sorry.
Well, I do it all offline in my fav text editor with, of course, AutoHotkey hotkeys. :) But a good way when new to the forum, as you are, is simply to click the double-quote mark in the upper right of the post to which you are replying:

Reply with quote.png
Reply with quote.png (1.93 KiB) Viewed 2011 times

That opens the quoted text in a compose window. You can leave the text as is or edit it if you want to respond to just a portion of the post, as I've done three times in this response. Regards, Joe

JuaninBeats
Posts: 21
Joined: 22 Jan 2022, 20:20

Re: Search for a file and open it

Post by JuaninBeats » 23 Jan 2022, 14:10

mikeyww wrote:
23 Jan 2022, 12:38
More ideas:

Code: Select all

base = C:\Ableton Projects
ext  = als

^a::
Send % ("^c", Clipboard := "")
ClipWait, 0
If !ErrorLevel {
 RegExMatch(Clipboard, "(.+)\((.+)\)", m), fn := Trim(m2) " - " Trim(m1) "." ext
 Loop, Files, %base%\%fn%, R
  Run, %A_LoopFilePath%
} Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return
This didn't work at all :( Can you check it please?

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

Re: Search for a file and open it

Post by mikeyww » 23 Jan 2022, 14:23

If your Clipboard isn't what you said it is, then the match will fail. You can display the value of fn to verify.

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Search for a file and open it

Post by JoeWinograd » 23 Jan 2022, 14:49

Hi Juan,
JuaninBeats wrote:This didn't work at all
Since that didn't work, and I'm not a RegEx expert (but I'm sure mikey will be on it), I'll take a run at it with standard (and, imo, easier to understand) string operations.

To be clear, the format of the selected text is always this:

Song (Artist)

And the corresponding file name is always this:

Artist - Song.als

So, here's code that finds the left and right parens:

Code: Select all

LeftParenPos:=InStr(haystack,"(")
If (LeftParenPos=0)
{
  ; error - text does not have a left paren
}
RightParenPos:=InStr(haystack,")")
If (RightParenPos=0)
{
  ; error - text does not have a right paren
}
Based on that, here's code that finds the artist and song:

Code: Select all

SongLength:=LeftParenPos-2
SongName:=SubStr(haystack,1,SongLength)
ArtistLength:=RightParenPos-LeftParenPos-1
ArtistName:=SubStr(haystack,LeftParenPos+1,ArtistLength)
And here's code that creates the file name (without path):

Code: Select all

FileName:=ArtistName . " - " . SongName . "." . FileExt
Here's a MsgBox from testing all that code:

juan test.png
juan test.png (5.08 KiB) Viewed 1993 times

I'll leave it to you to try to incorporate that code into my script, but if you need help on it, let me know. Regards, Joe

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

Re: Search for a file and open it

Post by mikeyww » 23 Jan 2022, 15:48

All good. I was thinking, too, that you might want to trim the clipboard as a first step.

JuaninBeats
Posts: 21
Joined: 22 Jan 2022, 20:20

Re: Search for a file and open it

Post by JuaninBeats » 23 Jan 2022, 16:17

Thank you again for all your help.
JoeWinograd wrote: To be clear, the format of the selected text is always this:

Song (Artist)

And the corresponding file name is always this:

Artist - Song.als

So, here's code that finds the left and right parens:
That's exactly how it is :+1:
But I don't know where to put this part of the code on the other one.
I'd appreciate that you can mix all of them so I can understand.

mikeyww wrote: If your Clipboard isn't what you said it is, then the match will fail. You can display the value of fn to verify.
The value that it shows me is correct, but it doesn't execute the file.

I think this would fix it:
Loop, Files, %base%\%fn%, R
If A_LoopFileName contains %fn%
Run, %A_LoopFilePath%
(second line). But doesn't execute the file neither. Can you help me figuring out what's happening?


Thanks again :-D

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

Re: Search for a file and open it

Post by mikeyww » 23 Jan 2022, 16:56

Sure. Just post your revision.

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Search for a file and open it  Topic is solved

Post by JoeWinograd » 23 Jan 2022, 17:12

JuaninBeats wrote:I'd appreciate that you can mix all of them so I can understand.
OK, I'll do it for you, but one of our goals in this forum is to teach folks enough so that they can be more self-sufficient, rather than simply writing scripts for them. I hope you're willing to dig in and learn.

Btw, mikey's idea on Trim is a very good one! In case you accidentally select some spaces before or after the desired text, his excellent idea will handle that, so I've included it in my script:

Code: Select all

RootFolder:="C:\Ableton Projects" ; root folder for all projects (without the ending backslash)
FileExt:="als" ; file extension for proejcts (without the dot)
^a:: ; Ctrl+a - imo, this is a bad choice for the hotkey, as it is the Windows standard for Select All - I suggest using a different hotkey
Clipboard:="" ; clear clipboard
SendInput,^c ; copy selected text to clipboard
ClipWait,0.1 ; wait 100ms for text to arrive on clipboard
If (ErrorLevel=1)
{
  MsgBox,4144,Error,Text did not appear on clipboard after 100ms
  Return
}
SearchTerm:=Trim(Clipboard)
LeftParenPos:=InStr(SearchTerm,"(")
If (LeftParenPos=0)
{
  MsgBox,4144,Error,Selected text does not have a left parenthesis
  Return
}
RightParenPos:=InStr(SearchTerm,")")
If (RightParenPos=0)
{
  MsgBox,4144,Error,Selected text does not have a right parenthesis
  Return
}
SongLength:=LeftParenPos-2
SongName:=SubStr(SearchTerm,1,SongLength)
ArtistLength:=RightParenPos-LeftParenPos-1
ArtistName:=SubStr(SearchTerm,LeftParenPos+1,ArtistLength)
FileName:=ArtistName . " - " . SongName . "." . FileExt
Loop,Files,%RootFolder%\*.%FileExt%,R ; recurse through subfolders
{
  If (A_LoopFileName=FileName) ; look for project name
  {
    Run,%A_LoopFilePath% ; found it - run it
    Return
  }
}
MsgBox,4144,Error - Not Found,Did not find a project with this in it:`n`n%FileName%
Return
Tested here and worked perfectly, but let me know if you have any problems. Regards, Joe

JuaninBeats
Posts: 21
Joined: 22 Jan 2022, 20:20

Re: Search for a file and open it

Post by JuaninBeats » 23 Jan 2022, 19:03

Thank you very much both of you @JoeWinograd @mikeyww !!
That last script works amazing 10/10!! Thank you :bravo: :dance:
JoeWinograd wrote:
23 Jan 2022, 17:12
OK, I'll do it for you, but one of our goals in this forum is to teach folks enough so that they can be more self-sufficient, rather than simply writing scripts for them. I hope you're willing to dig in and learn.

Don't worry, I'm here to learn, and I had learnt a lot copying how others make scripts :geek: So, now I have it, and with all your kind explanation, I can understand it and do my own variations.

Kind regards <3

JuaninBeats
https://twitch.tv/JuaninBeats :mrgreen:

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Search for a file and open it

Post by JoeWinograd » 23 Jan 2022, 19:37

JuaninBeats wrote:That last script works amazing 10/10!! Thank you
Glad to hear it! Thanks for letting us know. You're very welcome.
I'm here to learn ... I can understand it and do my own variations
Glad to hear that, too! Regards, Joe

Post Reply

Return to “Ask for Help (v1)”