使用 Spaces 在 Java 中创建新的 Firefox 配置文件?

Create New Firefox Profile in Java with Spaces?

我可以使用以下命令在自定义目录中轻松创建新的 Firefox 配置文件:

    String profileName = "ABC XYZ";
    Runtime rt = Runtime.getRuntime();

    String dir = "C:\My Custom Dir\profileName";


    String executeCommand = "cmd.exe /c start firefox -CreateProfile \""  +  profileName.replaceAll("\s", "_")  + " " + customFirefoxProfileDir + "\"";

    Process process = rt.exec(executeCommand);  

我遇到的问题是,如果我不将 profileName 中的空格替换为下划线(或其他一些字符),将仅使用 [=] 中的第一个单词生成配置文件12=](即 ABC 而不是 ABC XYZ)。

经过一段时间的研究,我认为这种奇怪的现象源于Firefox does not allow spaces in the Profile Name

-CreateProfile profile_name

Create a new profile in the default directory, but do not start the application. The profile will be named profile_name in the profile manager, the profile_name must not contain spaces().

虽然我总是可以自动用下划线替换空格,但问题是我无法强制执行此约定,因为某些配置文件是通过带空格的“选择用户配置文件”对话框手动创建的。

奇怪的是,当在 Firefox 中单击 Create Profile - 选择用户配置文件对话框时,我可以随意命名 - 包括 带空格 .

所以我的问题是:有没有办法以编程方式(在 Java 中)创建新的 Firefox 配置文件,同时在实际配置文件名称中包含空格?

我尝试用双引号将 profileName 括起来,但没有用。

您可以尝试使用不同于“键盘空格键”的空白字符。

您可能想查看此主题中的前 2 个答案: All the Whitespace Characters? Is it language independent?

B.t.w。这是 21 年(!)的旧错误条目,解释了为什么这是不可能的:https://bugzilla.mozilla.org/show_bug.cgi?id=51509

但也许您可以从这个问题的已接受答案中了解更多信息?

Programmatically create Firefox profiles

在任何情况下,我都建议使用 runtime.exec 的替代方法,该方法采用数组以避免弄乱额外的引号:

rt.exec(new String[] {"mkdir", "C:\my test"});

并且怀疑用 sysinternals procmon 观察屏幕背后到底发生了什么。

还可以考虑动态编写 .bat.cmd 或类似的脚本文件,您可以从 Java 执行这些文件,并且更易于编写和调试。 createprofile.cmd 文件示例:

@echo off
REM Put REM before the first line if you want to debug this script

IF "%~1"=="" ( echo please pass two arguments: the profile name and directory && goto END )
IF "%~2"=="" ( echo please pass two arguments, the profile name and directory && goto END )

REM Use %~1 instead of %1 to get rid of the double quotes of the invocation
set "FF_PROFILE_NAME=%~1"
set "FF_PROFILE_DIR=%~2"

IF exist "%FF_PROFILE_DIR%" ( echo directory already exists ) ELSE ( mkdir "%FF_PROFILE_DIR%" || exit %ERRORLEVEL%)

REM See: http://kb.mozillazine.org/Profiles.ini_file
set "PROFILE_CONFIG=%APPDATA%\Mozilla\Firefox\profiles.ini"

REM Let's make a backup first
copy "%PROFILE_CONFIG%" "%PROFILE_CONFIG%.bak-%RANDOM%%RANDOM%" >NUL

REM Find the highest profile number
REM TODO: Performance, stop looking if we found the highest profilenumber
REM TODO: Stop with error if we reach the highest profile number that we check for
FOR /L %%G IN (0,1,99) DO (findstr "\[Profile%%G\]" "%APPDATA%\Mozilla\Firefox\profiles.ini" >NUL && set "LAST_PROFILE=%%G")

SET /a NEXT_PROFILE=%LAST_PROFILE%+1

REM Write the new configuration section the ini file, appending it to the end:
ECHO.>> %PROFILE_CONFIG%
ECHO [Profile%NEXT_PROFILE%]>> %PROFILE_CONFIG%
ECHO Name=%FF_PROFILE_NAME%>> %PROFILE_CONFIG%
REM Escape with caret ^ the meaning of file descriptor numbers
ECHO IsRelative=^0>> %PROFILE_CONFIG%
ECHO Path=%FF_PROFILE_DIR%>> %PROFILE_CONFIG%
ECHO.>> %PROFILE_CONFIG%

:END