[Mobile] Dynamic mobile pulldown action using CodeScript Toolkit

I’ve been kinda obsessed with CodeScript Toolkit lately. With the ability to set scripts as “invocable,” you can set a script to your mobile pulldown action.

Here’s a script that:

  • If in a markdown file
    • If not in editing mode
    • If in editing mode
      • If a tablet
        • Open Quick Switcher++
      • If a phone
        • Toggle voice recording (start recording if not recording, stop recording if recording)
  • If in a Canvas file
    • If a tablet
      • Open Quick Switcher++
    • If a phone
  • If not in a markdown nor in a Canvas file
    • Open Quick Switcher++

You might be thinking all that “if a tablet → open Quick Switcher++” is redundant and you’re right, but it’s set up like that in case I have better ideas and want to change it later. I have a foldable phone so I wanted to have the pulldown do different things when my phone is unfolded.

Anyway, here’s the script!

exports.invoke = async (app) => {
	const currentfileextension = app.workspace?.activeLeaf?.view?.file?.extension
	if (currentfileextension === 'md') {
		const mode = app.workspace.activeLeaf.view.currentMode?.type;
		if (!mode) return;
	
		if (mode !== 'source') {
			if (document.body.classList.contains('is-tablet')) {
				app.commands.executeCommandById('darlal-switcher-plus:switcher-plus:open');
			} else {
				app.commands.executeCommandById('workspace:close');
			}
			return;
		}
	
		if (document.body.classList.contains('is-tablet')) {
			app.commands.executeCommandById('darlal-switcher-plus:switcher-plus:open');
		} else {
			const audioPlugin = app.internalPlugins.plugins['audio-recorder']?.instance;
			const isRecording = Boolean(audioPlugin?.recording);
		
			if (isRecording) {
				app.commands.executeCommandById('audio-recorder:stop');
				new Notice('⏹️ Recording stopped');
			} else {
				app.commands.executeCommandById('audio-recorder:start');
				new Notice('🔴 Recording started');
			}
		}
		return;
	}
	
	if (currentfileextension === 'canvas') {
		if (document.body.classList.contains('is-tablet')) {
			app.commands.executeCommandById('darlal-switcher-plus:switcher-plus:open');
		} else {
			app.commands.executeCommandById('editor:open-search');
		}
		return;
	}
	
	app.commands.executeCommandById('darlal-switcher-plus:switcher-plus:open');
};
3 Likes

This info really helps me.