MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey-v1.0 / FileSelectFile

Method FileSelectFile

Source/script2.cpp:8709–8955  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8707
8708
8709ResultType Line::FileSelectFile(char *aOptions, char *aWorkingDir, char *aGreeting, char *aFilter)
8710{
8711 Var *output_var = ResolveVarOfArg(0);
8712 if (!output_var)
8713 return FAIL;
8714 g_ErrorLevel->Assign(ERRORLEVEL_ERROR); // Set default ErrorLevel.
8715 if (g_nFileDialogs >= MAX_FILEDIALOGS)
8716 {
8717 // Have a maximum to help prevent runaway hotkeys due to key-repeat feature, etc.
8718 MsgBox("The maximum number of File Dialogs has been reached." ERR_ABORT);
8719 return FAIL;
8720 }
8721
8722 // Large in case more than one file is allowed to be selected.
8723 // The call to GetOpenFileName() may fail if the first character of the buffer isn't NULL
8724 // because it then thinks the buffer contains the default filename, which if it's uninitialized
8725 // may be a string that's too long.
8726 char file_buf[65535] = ""; // Set default.
8727
8728 char working_dir[MAX_PATH];
8729 if (!aWorkingDir || !*aWorkingDir)
8730 *working_dir = '\0';
8731 else
8732 {
8733 strlcpy(working_dir, aWorkingDir, sizeof(working_dir));
8734 DWORD attr = GetFileAttributes(working_dir);
8735 if (attr == 0xFFFFFFFF || !(attr & FILE_ATTRIBUTE_DIRECTORY))
8736 {
8737 // Above condition indicates it's either an existing file or an invalid
8738 // folder/filename (one that doesn't currently exist). In light of this,
8739 // it seems best to assume it's a file because the user may want to
8740 // provide a default SAVE filename, and it would be normal for such
8741 // a file not to already exist.
8742 char *last_backslash = strrchr(working_dir, '\\');
8743 if (last_backslash)
8744 {
8745 strlcpy(file_buf, last_backslash + 1, sizeof(file_buf)); // Set the default filename.
8746 *last_backslash = '\0'; // Make the working directory just the file's path.
8747 }
8748 else // the entire working_dir string is the default file.
8749 {
8750 strlcpy(file_buf, working_dir, sizeof(file_buf));
8751 *working_dir = '\0'; // This signals it to use the default directory.
8752 }
8753 }
8754 // else it is a directory, so just leave working_dir set as it was initially.
8755 }
8756
8757 char greeting[1024];
8758 if (aGreeting && *aGreeting)
8759 strlcpy(greeting, aGreeting, sizeof(greeting));
8760 else
8761 // Use a more specific title so that the dialogs of different scripts can be distinguished
8762 // from one another, which may help script automation in rare cases:
8763 snprintf(greeting, sizeof(greeting), "Select File - %s", g_script.mFileName);
8764
8765 // The filter must be terminated by two NULL characters. One is explicit, the other automatic:
8766 char filter[1024] = "", pattern[1024] = ""; // Set default.

Callers

nothing calls this directly

Calls 7

MsgBoxFunction · 0.85
strlcpyFunction · 0.85
snprintfFunction · 0.85
StrReplaceAllFunction · 0.85
ATOIFunction · 0.85
AssignMethod · 0.80
IsWin2000orLaterMethod · 0.80

Tested by

no test coverage detected