#creating a .lnk shortcut file

3 messages · Page 1 of 1 (latest)

mild bear
#

this works perfectly, but i just feel like theres a better way of doing this (also im only making this for windows)

export async function updateStartupSetting(startOnStartup: boolean) {
    const startupPath = await path.resolve(await path.appDataDir(), '..', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup');
    const exePath = await invoke<string>('current_exe');
    
    if (!(await fs.exists(startupPath))) {
        await message(`Startup apps folder does not exist! Checked:\n${startupPath}`, { title: 'Studio Plugin Enhancer', kind: 'error' });
        return;
    }
    
    const shortcutPath = await path.join(startupPath, 'StudioPluginEnhancer.lnk');
    
    if (startOnStartup) {
        if (await fs.exists(shortcutPath)) {
            return;
        }
        
        const escapedShortcutPath = shortcutPath.replace(/\\/g, '\\\\');
        const escapedExePath = exePath.replace(/\\/g, '\\\\');
        const workingDirectory = (await path.dirname(exePath)).replace(/\\/g, '\\\\');
        
        const command = Command.create('powershell');
        
        command.on('close', async (status) => {
            if (status.code !== 0) {
                await message(`Failed to create shortcut with exit code ${status.code} at:\n${shortcutPath}`, { title: 'Studio Plugin Enhancer', kind: 'error' });
            }
        });
        
        const process = await command.spawn();
        
        await process.write(`$WshShell = New-Object -ComObject WScript.Shell\n`);
        await process.write(`$Shortcut = $WshShell.CreateShortcut('${escapedShortcutPath}')\n`);
        await process.write(`$Shortcut.TargetPath = '${escapedExePath}'\n`);
        await process.write(`$Shortcut.WorkingDirectory = '${workingDirectory}'\n`);
        await process.write(`$Shortcut.Save()\n`);
        await process.write('exit\n');
    } else {
        if (await fs.exists(shortcutPath)) {
            await fs.remove(shortcutPath);
        }
    }
}

https://stackoverflow.com/questions/9701840/create-a-shortcut-lnk-file-using-powershell is where i got that powershell script

thorn relic
#

Maybe you can solve that without having to write a shortcut by using the autostart plugin?

Tauri

Automatically launch your app at system startup.

mild bear
#

oh my god wait how did i not see that