Need help! Button Script -- Open Newest File in Source Directory

I've managed to piece together a VBscript that will open the newest modified file in a hardcoded directory. Now I want this to work as a DOpus button (using the source directory) and include a confirmation dialog where F5 would accept and Esc would cancel the file opening. Meaning: hit shortcut once for prompting and twice for opening, or Esc to cancel the deal. That's it. Would be so thankful for some help with this...

Here's my original working script:

' ----------------------------------------------------------------------------
' Find the newest file (modified) in specific dir
' ----------------------------------------------------------------------------

' Using code from https://www.tek-tips.com/viewthread.cfm?qid=1476914

Set objFSO = CreateObject("Scripting.FileSystemObject")

FolderToScan = "D:\Dropbox\3D\Projects\_Blender Projects\Rowboat\"

Set objFolder = objFSO.GetFolder(FolderToScan)

NewestFile = ""
NewestDate = #1/1/1970#

For Each objFile In objFolder.Files
    If objFile.DateLastModified > NewestDate Then
        NewestDate = objFile.DateLastModified
        NewestFile = objFile.Name
    End If
Next

'WScript.Echo NewestFile


' ----------------------------------------------------------------------------
' Open the file
' ----------------------------------------------------------------------------

' Using code from https://customerfx.com/article/how-to-execute-a-file-program-in-vbscript/

Sub Run(ByVal sFile)
Dim shell
    Set shell = CreateObject("WScript.Shell")
    shell.Run Chr(34) & sFile & Chr(34), 1, false
    Set shell = Nothing
End Sub

Run FolderToScan & NewestFile

The confirmation dialog I'd like some help implementing (I'm confused how I would mix and match VBScript and DOpus commands):

open_confirm_dialog

And here's the work-in-progress button: Open Newest File.dcf (3.0 KB)


TLDR {
To me, the use case is that I have working folders with TONS of file iterations and I don't want to scroll down the list and click the latest file every time I want to continue work – I just want to press a hotkey once, to bring up the dialog and twice (same hotkey), to open the latest file. This would save me the equivalent energy of lifting two elephants with my pointing finger in the long run. If I can find some help getting this to work, I intend to share the button in the scripts section, since I believe others will greatly benefit from it too. F5 > Question > F5 > BAM! ...Off to the races.
} TLDR

The default script, when you create a new button and set it to script mode, shows you how to get the current directory and various other things.

Which particular aspect do you need help with?

I'm really not a scripter and would need help assigning the FolderToScan in the code to the Source folder. As well as adding the confirmation dialog asking whether user want to open the file or not, using the same hotkey assigned to the button, to confirm.

Would not this

Select NONE
Select DATE=newest TYPE=files SETFOCUS
Select SHOWFOCUS

do the job?

That's almost there! I just want to add the confirmation dialog on top of that (see first post). I'll try reading the docs and see if I can accomplish what I want using DOpus internal commands ...

You wouldn't need the confirmation dialog since nothing would be launched until you pushed return, after looking at what was selected to check it's what you want.

@leo You're right. It's a personal preference. I'm going for optimal ergonomic efficiency here :wink:

F5→F5 has a seemingly slight edge over Button/Hotkey→Enter. Devil's in the details but my wished-for implementation won't physically scroll down the list; it would ask me to confirm so I won't open any potential garbage file … and the travel from F5 to Enter is a mile's leap by any VIM standard :smiley: (Make of it what you will, this is how I am. And yes, OCD is my last name.)

Even when using two hands?

Plus, Esc is not exactly around the corner, either :wink:

But well, if you want to use F5 as a magic double hotkey, I don't think there is a way around using a script.

For anyone curious whether I managed to accomplish my goal or not. I did, after pulling some hair and ditching the (way over my head) VBScript side of things. I resorted to my trusty old AutoHotkey to sort it out. So now I have this patented killer set up:

single-tap F5: Focus newest file in source directory
double-tap F5: Open newest file -- '' --
single-tap F6: Focus newest folder -- '' --
double-tap F6: Open newest folder -- '' --

Fantastic! Should anyone want the same, here's the AutoHotkey code. You'll also have to create buttons in DOpus and bind them to desired keys (I used F5 and F6). I'll share my buttons for convenience as well.

Save the following in an AutoHotkey file, e.g. Hotkeys.ahk:

; Directory Opus
; ----------------------------------------------------------------------------
#IfWinActive ahk_exe dopus.exe ahk_class dopus.lister

; Open newest file in source directory <= F5 F5 (tap twice within set amount of ms)
F5::
Send {F5}
if (f5_presses > 0) ; SetTimer already started, so we log the keypress instead.
{
    f5_presses += 1
    return
}
; Otherwise, this is the first press of a new series. Set count to 1 and start
; the timer:
f5_presses := 1
SetTimer, KeyF5, -300  ; Wait for more presses within a 300 millisecond window
return

; Open newest folder in source directory <= F6 F6 (tap twice within set amount of ms)
F6::
Send {F6}
if (f6_presses > 0) ; SetTimer already started, so we log the keypress instead.
{
    f6_presses += 1
    return
}
; Otherwise, this is the first press of a new series. Set count to 1 and start
; the timer:
f6_presses := 1
SetTimer, KeyF6, -300 ; Wait for more presses within a 300 millisecond window
return

KeyF5:
if (f5_presses = 1) ; The key was pressed once
{
    Send {F5}
}
else if (f5_presses = 2) ; The key was pressed twice
{
    Send {Enter}
}
else if (f5_presses > 2)
{
    ; Three or more clicks detected; optional code here
}
; Regardless of which action above was triggered, reset
; the count to prepare for the next series of presses:
f5_presses := 0
return

KeyF6:
if (f6_presses = 1) ; The key was pressed once
{
    Send {F6}
}
else if (f6_presses = 2) ; The key was pressed twice
{
    Send {Enter}
}
else if (f6_presses > 2)
{
    ; Three or more clicks detected; optional code here
}
; Regardless of which action above was triggered, reset
; the count to prepare for the next series of presses:
f6_presses := 0
return

#IfWinActive

Here's how I've placed the buttons in my DOpus:

Here are the buttons (using handy snippet from @lxp above). Remember to bind them to desired keys and use the same keys in both the AutoHotkey script and DOpus:

Select_Open Newest File.dcf (617 Bytes)
Select_Open Newest Folder.dcf (620 Bytes)

1 Like

Very nifty! I always thought I could benefit from AHK, but my scripting never gained traction. Maybe I give it another try.

1 Like