Need Help Converting my Script Into Python to Suppress Command Window

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
tolagarf
Posts: 18
Joined: 20 Sep 2017, 08:02

Need Help Converting my Script Into Python to Suppress Command Window

Post by tolagarf »

I've been using my Filebot script executed by uTorrent for a long time and it's been running great. However lately I've noticed more games don't like it when these command windows popup, even though I try to suppress it with the "cmd /c START /MIN cmd /c filebot.exe". So I've been considering converting it to Python when I saw a thread here on the forums detailing how to do it, but as always I'm rather clueless about these scripts how to do it, as I didn't produce my own script for Filebot, and some nice people here on the forums helped me with that. Now I need help again and hope someone has the time to 'enlighten' me on how to do this :)

viewtopic.php?t=5157

This is the script I use for uTorrent to execute:

Code: Select all

filebot.exe -script fn:amc --output "Y:/Downloads" --action copy --conflict skip -non-strict "Y:/!torrent" --log-file "Y:/Downloads/amc.log" --def excludeList=amc.txt --def movieFormat="{genres.contains(/Documentary/) ? 'X:/Docus/' : 'X:/Movies/0/'}/{plex.name}/{plex.name}{'.'+vf}{'.'+vc}{'.'+ac}{'-'+group}" seriesFormat="{genres.contains(/Documentary/) ? 'X:/Docus/' : 'X:/TV Shows/1/'}{plex.tail}{'.'+source}{'.'+vf}{'.'+vc}{'.'+ac}{'-'+group}" animeFormat="X:/TV Shows/1/'}{plex.tail}{'.'+source}{'.'+vf}{'.'+vc}{'.'+ac}{'-'+group}" musicFormat="{genres.contains(/Audiobook/) ? 'X:/Audiobooks/' : 'X:/Music/'}{albumartist}/{album}/{t}" unsorted=y music=y extractFolder="Y:/Downloads/Unsorted" artwork=n deleteAfterExtract=y clean=y reportError=y gmail=account:password
Somehow that needs to be converted into this, and I'm completely lost here:

Code: Select all

# Run Program: 
# "C:\path\to\utorrent-postprocess.pyw" "%L" "%S" "%N" "%K" "%F" "%D"
#
# Test with Console:
# python "C:\path\to\utorrent-postprocess.pyw" "Movie" "5" "Avatar" "multi" "" "X:\Files\Avatar"


import sys
import subprocess


# configuration
output = 'X:/Media'


# custom formats (use / instead of \ as directory separator)
movieFormat  = '''{plex}'''
seriesFormat = '''{plex}'''
animeFormat  = '''{plex}'''
musicFormat  = '''{plex}'''


# required args
label, state, title, kind, file, directory = sys.argv[1:7]


command = [
	'filebot', '-script', 'fn:amc',
	'--output', output,
	'--action', 'duplicate',
	'--conflict', 'skip',
	'-non-strict',
	'--log-file', output + '/amc.log',
	'--def',
		'unsorted=y',
		'music=y',
		'artwork=y',
		'movieFormat='  + movieFormat,
		'seriesFormat=' + seriesFormat,
		'animeFormat='  + animeFormat,
		'musicFormat='  + musicFormat,
		'ut_label=' + label,
		'ut_state=' + state,
		'ut_title=' + title,
		'ut_kind='  + kind,
		'ut_file='  + file,
		'ut_dir='   + directory
]


# execute command only for certain conditions (disabled by default)
'''
if state not in ['5', '11']:
	print('Illegal state: %s' % state)
	sys.exit(0)
'''


# execute command (and hide cmd window)
subprocess.run(command, creationflags=0x08000000)
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need Help Converting my Script Into Python to Suppress Command Window

Post by rednoah »

I suppose you could just use the runw script to run any command with CMD suppressed, including your existing one:
https://github.com/filebot/plugins/blob ... t/runw.pyw


e.g.

Code: Select all

C:/path/to/pythonw.exe C:/path/to/runw.pyw C:/path/to/filebot.exe -script fn:amc ...
:idea: You may or may not need absolute paths for all the executables or scripts depending on your %PATH% environment.
:idea: Please read the FAQ and How to Request Help.
tolagarf
Posts: 18
Joined: 20 Sep 2017, 08:02

Re: Need Help Converting my Script Into Python to Suppress Command Window

Post by tolagarf »

Thank you, I will certainly look into that (and didn't know that was a thing either) :)
tolagarf
Posts: 18
Joined: 20 Sep 2017, 08:02

Re: Need Help Converting my Script Into Python to Suppress Command Window

Post by tolagarf »

rednoah wrote: 31 Jan 2020, 17:08 I suppose you could just use the runw script to run any command with CMD suppressed, including your existing one:
https://github.com/filebot/plugins/blob ... t/runw.pyw


e.g.

Code: Select all

C:/path/to/pythonw.exe C:/path/to/runw.pyw C:/path/to/filebot.exe -script fn:amc ...
:idea: You may or may not need absolute paths for all the executables or scripts depending on your %PATH% environment.
Sorry, but I cannot get this working at all. Does it require a Python version greater than v2.7.x ?
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need Help Converting my Script Into Python to Suppress Command Window

Post by rednoah »

I don't know. Depends on what the error message says.

:!: Since we're using pythonw we disable the console, and thus don't see error messages. For testing, we want to use python, and possibly remove creationflags=0x08000000 in the script so we get complete console log from the filebot call as well.
:idea: Please read the FAQ and How to Request Help.
tolagarf
Posts: 18
Joined: 20 Sep 2017, 08:02

Re: Need Help Converting my Script Into Python to Suppress Command Window

Post by tolagarf »

Alright got this working now with Python 3.8.1. I was using Python 2.7.x before that and got this error message:

Code: Select all

C:\Users\TFL>python F:/TFL/Onedrive/Tools/runw.pyw F:/TFL/Onedrive/Tools/dpclat.exe
Traceback (most recent call last):
  File "F:/TFL/Onedrive/Tools/runw.pyw", line 5, in <module>
    subprocess.run(sys.argv[1:], creationflags=0x08000000)
AttributeError: 'module' object has no attribute 'run'
So tested it with Python 3.8.1 and seems to be working as intended, although not all programs with launch unless you run Python as elevated, and that in itself presents a problem since you now get that annoying popup with Windows 10 asking for your permission to run the file. However in the case with Filebot, no issues there.
Post Reply