All pastes #22053 Raw Edit

vhaarr

public python v1 · immutable
#22053 ·published 2005-09-03 18:11 UTC
rendered paste body
#!/usr/bin/python# "Send files to contact" script for nautilus-actions# Make a new action in NACT that has the parameter "%s %d %m" to use this# script.# Author: Vidar Braut Haarr <vhaarr+trap@gmail.com># $1 should be the scheme, like "file"# $2 should be the path, like "/path/to"# $3-X should be the filenames# Configure these to your system:MOZ_TB = "/usr/bin/mozilla-thunderbird"MOZ_TB_XREMOTE = "/usr/lib/mozilla-thunderbird/mozilla-thunderbird-xremote-client"DISPLAY = ":0.0"def fileExists(filename):	try:		file = open(filename)	except IOError:		exists = 0	else:		exists = 1	return existsdef printUsageAndExit():	sys.stdout.write('usage: sendtoemail.py [file|sftp|ftp|smb|dav] [DIRECTORY] [FILENAME]...\n' \                         'Directory is the directory that holds all the files. The script does not\n' \                         'support sending files from multiple directories.\n\n' \                         'The script was made to be used with nautilus-actions 0.5. Configure it\n' \                         'with command parameters \'%s %d %m\', and it should work out of the box.\n\n' \                         'sendtoemail.py launches Mozilla Thunderbird, opening a new compose\n' \                         'window with the specified files attached to the new e-mail. If Mozilla\n' \                         'Thunderbird is already running, sendtoemail.py uses it through XRemote\n' \                         'instead of launching a new instance.\n\n' \                         'Author Vidar Braut Haarr <vhaarr+trap@gmail.com>\n')	sys.exit()import sysimport osimport stringcmdList = sys.argv# Just pop one to get the script name $0 out of the queueSCRIPTNAME = cmdList.pop(0)# Check to see that we have at least 3 arguments:# <scheme> <path> <file1>...<fileX>if len(cmdList) < 3:	sys.stderr.write(SCRIPTNAME + ': Not enough parameters.\n')	printUsageAndExit()# Check that the given scheme is supportedsupportedSchemes = ['file','sftp','smb','ftp','dav']scheme = cmdList.pop(0)if scheme in supportedSchemes == False:	sys.stderr.write(SCRIPTNAME + ': ' + scheme + ' is not a supported scheme.\n')	printUsageAndExit()scheme = scheme + "://"# Check that the given path is a directory that existspath = cmdList.pop(0)if os.path.exists(path) == False:	sys.stderr.write(SCRIPTNAME + ': ' + path + ' does not exist.\n')	printUsageAndExit()if fileExists(MOZ_TB) == 0:	sys.stderr.write(SCRIPTNAME + ': ' + MOZ_TB + ' not found.\n')	sys.exit()if fileExists(MOZ_TB_XREMOTE) == 0:	sys.stderr.write(SCRIPTNAME + ': ' + MOZ_TB_XREMOTE + ' not found.\n')	sys.exit()# Need to compensate for files with one or more spaces in the filename.# ... after some research, it seems that we have to compensate for# files having %20 as well. The first file we get can have spaces, but# file 2-> has %20 instead ... this must be nautilus[-actions]'s doing.arguments = "attachment='"tmpFilename = ""filename = ""for arg in cmdList:	# If we're not building a filename currently,	if tmpFilename == "":		# Just check for this argument		filename = path + "/" + arg	else:		# We're building one; add this arg to the building string		filename = tmpFilename + " " + arg	if fileExists(string.replace(filename,"%20"," ")) == 1:		# Alright, we have a file - add it to arguments and clear building string.		arguments = arguments + scheme + string.replace(filename," ","%20") + ","		tmpFilename = ""		filename = ""	else:		# File doesn't exist, lets build on it with more args.		tmpFilename = filenamearguments = arguments + "'"# Now, check if TB is running already.# 0 means it's running# 512 means it's not runningretval = os.system(MOZ_TB_XREMOTE + " -a 'mozilla-thunderbird' 'ping()'")if retval == 0:	os.system(MOZ_TB_XREMOTE + " \"xfeDoCommand(composemessage," + arguments + ")\"")elif retval == 512:	os.system(MOZ_TB + " --display=" + DISPLAY + " -compose \"" + arguments + "\"")else:	sys.stderr.write(SCRIPTNAME + ": Return value " + retval + " from " + MOZ_TB_REMOTE + " unknown. Accepted values are 0 and 512.")# EOF