Skip to content

The swiss army knife of lossless video/audio editing

Fix --keyboard-action edge cases

Claude19bd79e33128
diff --git a/docs/cli.md b/docs/cli.md
index d3484874..66d92fa2 100644
--- a/docs/cli.md
+++ b/docs/cli.md
@@ -62,7 +62,7 @@ LosslessCut file1.mp4 file2.mkv
### Keyboard actions, `--keyboard-action`
-Simulate a keyboard press action in an already running instance of LosslessCut. **Note that the command will return immediately**, so if you want to run multiple actions in a sequence, you have to `sleep` for a few seconds between the commands. Alternatively if you want to wait until an action has finished processing, use the [HTTP API](api.md) instead.
+Simulate a keyboard press action in an already running instance of LosslessCut (if no instance is running, the action is ignored). Arguments are JSON or JSON5. **Note that the command will return immediately**, so if you want to run multiple actions in a sequence, you have to `sleep` for a few seconds between the commands. Alternatively if you want to wait until an action has finished processing, use the [HTTP API](api.md) instead.
### Available keyboard actions
diff --git a/src/main/index.ts b/src/main/index.ts
index 63212601..c278e833 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -25,7 +25,7 @@ import mitt from 'mitt';
import logger from './logger.js';
import menu from './menu.js';
import * as configStore from './configStore.js';
-import { isLinux, isWindows, isMac, platform, arch, pathExists, resolveCliFileArgs, resolveCliKeyboardActionArgs, parseSettingsJson } from './util.js';
+import { isLinux, isWindows, isMac, platform, arch, pathExists, resolveCliFileArgs, parseCliKeyboardActionArgs, parseSettingsJson } from './util.js';
import { appName } from './common.js';
import attachContextMenu from './contextMenu.js';
import HttpServer from './httpServer.js';
@@ -323,8 +323,13 @@ async function init() {
logger.info('second-instance', argv2);
const { keyboardAction } = argv2;
- if (keyboardAction) sendApiAction(keyboardAction, resolveCliKeyboardActionArgs(keyboardAction, argv2._.map((arg) => JSON.parse(String(arg))), workingDirectory));
- else if (argv2._ && argv2._.length > 0) openFilesEventually(resolveCliFileArgs(argv2._.map(String), workingDirectory));
+ if (keyboardAction) {
+ try {
+ sendApiAction(keyboardAction, parseCliKeyboardActionArgs(keyboardAction, argv2._, workingDirectory));
+ } catch (err) {
+ logger.error('Invalid --keyboard-action arguments', err);
+ }
+ } else if (argv2._ && argv2._.length > 0) openFilesEventually(resolveCliFileArgs(argv2._.map(String), workingDirectory));
});
// Quit when all windows are closed.
@@ -378,7 +383,8 @@ async function init() {
logger.info('CLI arguments', argv);
// Only if no files to open already (open-file might have already added some files)
- if (filesToOpen.length === 0) filesToOpen = argv._.map(String);
+ if (argv['keyboardAction'] != null) logger.warn('--keyboard-action only works with an already running instance, ignoring it and its arguments');
+ else if (filesToOpen.length === 0) filesToOpen = argv._.map(String);
const { settingsJson } = argv;
setDisableNetworking(argv['disableNetworking']);
diff --git a/src/main/util.test.ts b/src/main/util.test.ts
index 45acd64c..5fb60818 100644
--- a/src/main/util.test.ts
+++ b/src/main/util.test.ts
@@ -2,7 +2,7 @@
import { describe, expect, test } from 'vitest';
import { join, resolve, sep } from 'node:path';
-import { parseSettingsJson, resolveCliFileArgs, resolveCliKeyboardActionArgs } from './util';
+import { parseCliKeyboardActionArgs, parseSettingsJson, resolveCliFileArgs } from './util';
// build platform-native absolute paths so the test also passes on Windows
const cwd = resolve('/home/user/videos');
@@ -18,16 +18,19 @@ describe('resolveCliFileArgs', () => {
});
});
-describe('resolveCliKeyboardActionArgs', () => {
+describe('parseCliKeyboardActionArgs', () => {
test('resolves openFiles paths against the given cwd', () => {
- expect(resolveCliKeyboardActionArgs('openFiles', [['video.mp4']], cwd)).toEqual([[join(cwd, 'video.mp4')]]);
+ expect(parseCliKeyboardActionArgs('openFiles', ['["video.mp4"]'], cwd)).toEqual([[join(cwd, 'video.mp4')]]);
});
- test('leaves other actions and malformed openFiles args untouched', () => {
- const args = [{ time: '12.23' }];
- expect(resolveCliKeyboardActionArgs('goToTimecodeDirect', args, cwd)).toEqual(args);
- const malformed = ['video.mp4'];
- expect(resolveCliKeyboardActionArgs('openFiles', malformed, cwd)).toEqual(malformed);
+ test('parses JSON and JSON5, leaving other actions and malformed openFiles args untouched', () => {
+ expect(parseCliKeyboardActionArgs('goToTimecodeDirect', ['{"time": "12.23"}'], cwd)).toEqual([{ time: '12.23' }]);
+ expect(parseCliKeyboardActionArgs('goToTimecodeDirect', ['{time: "12.23"}'], cwd)).toEqual([{ time: '12.23' }]);
+ expect(parseCliKeyboardActionArgs('openFiles', ['"video.mp4"'], cwd)).toEqual(['video.mp4']);
+ });
+
+ test('throws on invalid JSON5', () => {
+ expect(() => parseCliKeyboardActionArgs('goToTimecodeDirect', ['{time:'], cwd)).toThrow();
});
});
diff --git a/src/main/util.ts b/src/main/util.ts
index 24c1303a..4299fcee 100644
--- a/src/main/util.ts
+++ b/src/main/util.ts
@@ -26,8 +26,9 @@ export async function pathExists(path: string) {
// https://github.com/mifi/lossless-cut/issues/1571
export const resolveCliFileArgs = (args: string[], workingDirectory: string) => args.map((arg) => (isAbsolute(arg) ? arg : resolve(workingDirectory, arg)));
-// `--keyboard-action openFiles '["file.mp4"]'` takes file paths too
-export function resolveCliKeyboardActionArgs(action: string, args: unknown[], workingDirectory: string) {
+// Throws if an arg is not valid JSON5. `--keyboard-action openFiles '["file.mp4"]'` takes file paths, so resolve those too
+export function parseCliKeyboardActionArgs(action: string, rawArgs: unknown[], workingDirectory: string) {
+ const args: unknown[] = rawArgs.map((arg) => JSON5.parse(String(arg)));
if (action !== 'openFiles') return args;
const parsed = openFilesActionArgsSchema.safeParse(args);
if (!parsed.success) return args;