How to HIDE application while script runs

I do not wish the application Quicktime to be visible while this script runs, however it is. Any help would be much appreciated.



tell application "Finder"
	set finderSelList to selection as alias list
end tell

tell application "Finder"
	set visible to false
	activate
	open document file finderSelList using application file "QuickTime Player 7.app" of folder "Utilities" of folder "Applications" of startup disk
end tell

delay 0.5
tell application "System Events" to keystroke "s" using command down
delay 0.5
tell application "System Events" to key code 36
delay 0.5
tell application "System Events" to keystroke "w" using command down



you have to start your application first in background, instead to wake her up and give instructions. It’s less rude :smiley:
Like so:

run application "QuickTime Player 7"

Hi Joy

Where exactly in the script would add this line? I tried a few and they did not work.

run application “QuickTime Player 7”

Thanks, David

Hi David.

Perhaps you should say what the script’s supposed to do. On the face of it, it opens one or more files that have been selected in the Finder, uses a keystroke to save whichever of the resulting documents happens to be frontmost back to its original file, performs another keying operation that I haven’t been able to identify, and finally uses another keystroke to close that particular document. It doesn’t make much sense.

If an application’s not frontmost and visible, keystrokes and key codes won’t be able to reach it. QuickTime Player 7 has open, save, and close commands in its scripting dictionary. If they work, you should consider using them instead.

The ultimate goal is to convert mp3 or mp4 files to quicktime movies as fast as possible. I tried using ffmpeg, but it is a few seconds slower than this method.

So in the apple script I would like it to open the selected file (from the finder) in Quicktime 7, then save the file as a .mov to the desktop. I would like this to all happen in the background so the user does not see any windows popping around.

Make sense?

David,
You use an older version of apple script and code behaves not the same from Os to Os. Anyway

set getMovies to (choose file default location (path to movies folder) with prompt "choose movie files to process" with multiple selections allowed) 
if getMovies is false then return

launch application "QuickTime Player 7"
delay 0.5
repeat with b in (getMovies as list) 
set b to (b as text) 
#do shell script "open -a 'QuickTime Player 7' " & quoted form of POSIX path of b
tell process "QuickTime Player 7" of application "System Events"
open file b 
#save-export movie in another format
end
end repeat

Untested script. And I don’t know if this commands work for you as you use another Os. If “open file” doesn’t work you can try to use the shell command but then all the stuff comes in front. As far as I can remember, QuickTime should have a scripting library, so you need not to go mad with UI scripting. Can’t check this myself, unfortunately.

I have QuickTime Player 7, but haven’t upgraded it to the Pro version, so the “Save…” and “Export…” menu items are greyed out and I can’t try them. However, these facilities are available through AppleScript.

There are several types of save in the dictionary, two of which produce independent, playable files which the Finder reports as “QuickTime movies”. But the results are vastly different in size and I don’t know if they both actually are QuickTime movies:
save self contained is very fast and saves a file similar in size to the original mp3 or mp4.
export … as QuickTime movie takes much longer and produces a much larger file. Because it takes so long, it drops a sheet over the document window giving the opportunity to abort the process. This makes the window visible again and there’s nothing that can be done about it.

Here’s a tested script:


main()

on main()
	set pathToDestinationFolder to (path to desktop as text)
	
	tell application "Finder"
		set finderSelList to selection as alias list
	end tell
	
	tell application "QuickTime Player 7"
		-- Open QuickTime Player 7 without bringing it to the front or displaying its default window.
		launch
		-- To minimise the visual effect, open all the selected files at once, then hide all the resulting windows at once.
		set openedDocuments to (open finderSelList)
		set visible of windows to false
		-- Save each document in turn, with modified names, to the destination folder.
		repeat with thisDoc in openedDocuments
			set docName to thisDoc's name
			set movName to my makeMovNameFrom(docName)
			-- export thisDoc to file (pathToDestinationFolder & movName) as QuickTime movie -- with replacing
			-- Or:
			save self contained thisDoc in file (pathToDestinationFolder & movName)
		end repeat
		quit
	end tell
end main

-- Given a document name, derive a new file name with a ".mov" extension.
on makeMovNameFrom(originalName)
	if (originalName contains ".") then
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "."
		set movName to (text 1 thru text item -2 of originalName) & ".mov"
		set AppleScript's text item delimiters to astid
	else
		set movName to originalName & ".mov"
	end if
	
	return movName
end makeMovNameFrom

Another approach, which produces files of a similar size to those with save self contained, is to dispense with QuickTime Player 7 altogether and use ASObjC with the AVFoundation framework instead. I’ve only researched this enough to produce a working script, so I don’t know how watertight it is:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AVFoundation"
use scripting additions

main()

on main()
	set |⌘| to current application
	set destinationFolderURL to |⌘|'s class "NSURL"'s fileURLWithPath:(POSIX path of (path to desktop))
	set completed to |⌘|'s AVAssetExportSessionStatusCompleted
	
	tell application "Finder"
		set finderSelList to selection as alias list
	end tell
	
	repeat with thisAlias in finderSelList
		-- Get an NSURL for this file.
		set originalFileURL to (|⌘|'s class "NSURL"'s fileURLWithPath:(POSIX path of thisAlias))
		-- Set the file to be an "asset" for an export session.
		set URLAsset to (|⌘|'s class "AVURLAsset"'s URLAssetWithURL:(originalFileURL) options:(missing value))
		-- Initialise a highest-quality export session.
		set exportSession to (|⌘|'s class "AVAssetExportSession"'s exportSessionWithAsset:(URLAsset) presetName:("AVAssetExportPresetHighestQuality"))
		-- Set its output file type to be QuickTime movie.
		tell exportSession to setOutputFileType:("com.apple.quicktime-movie")
		-- Get the name of the current input file and derive an output name for it with a ".mov" extension.
		set originalName to originalFileURL's lastPathComponent()
		if ((originalName's containsString:(".")) as boolean) then
			set movName to (originalName's stringByReplacingOccurrencesOfString:("\\.[^.]*+$") withString:(".mov") options:(|⌘|'s NSRegularExpressionSearch) range:({0, originalName's |length|()}))
		else
			set movName to (originalName's stringByAppendingString:(".mov"))
		end if
		-- Append the ouput name to the destination folder URL.
		set destinationFileURL to (destinationFolderURL's URLByAppendingPathComponent:(movName))
		-- Set that as the destination for the export session.
		tell exportSession to setOutputURL:(destinationFileURL)
		
		-- Perform the export and wait for it either to complete or fail.
		-- NB. NO COMPLETION HANDLER BLOCK IS PROVIDED HERE AS ASOBJC DOESN'T DO BLOCKS. THIS WORKS IN MY TESTS, BUT I DON'T KNOW IF IT'S A GOOD IDEA!
		tell exportSession to exportAsynchronouslyWithCompletionHandler:(missing value)
		repeat while (exportSession's status() < completed)
			delay 0.2
		end repeat
	end repeat
end main

Hi Nigel

This first script is pretty close and outrageously fast. Particularly if I cut the script off after ‘end main’ but I am not sure how set the new file name to the original file name & .mov. The document name is different from the file name, which I would like to retain.

Yes. That would tend to make things stop very quickly. :wink:

Apart from restoring the handler after ‘end main’? And do you mean with “.mov” instead of the original extension — which is what the script does now — or in addition to it?

I don’t see how that’s possible. Could you give an example?

When I just used the script to this point and hardcoded the movName to “xxx.mov” it was soooo fast. But ideally I would retain full file name. The file name is “08 Champion.m4a” but the document name was simply “Champion.” If I opened the inspector in QT I see that is true…weird but true. Is there any way to acquire the filename quickly. BTW there the user will not be selecting multiple files in this workflow.

Really appreciate all your help as this has been killing me for a while.

Sure looks fine to me. That approach to work around blocks works well enough elsewhere.

Hi David.

It think the safest and most convenient approach would be to extract the file’s name from the document’s path and to combine that with changing the extension to “.mov”. On my machine, the path is a POSIX one (with slashes) rather than an HFS one (with colons), so I’ve coded for that. But it’s easy to change if it’s different for you:


main()

on main()
	set pathToDestinationFolder to (path to desktop as text)
	
	tell application "Finder"
		set finderSelList to selection as alias list
	end tell
	
	tell application "QuickTime Player 7"
		-- Open QuickTime Player 7 without bringing it to the front or displaying its default window.
		launch
		-- To minimise the visual effect, open all the selected files at once, then hide all the resulting windows at once.
		set openedDocuments to (open finderSelList)
		set visible of windows to false
		-- Save each document in turn, with modified names, to the destination folder.
		repeat with thisDoc in openedDocuments
			set docPath to thisDoc's path
			set movName to my makeMovNameFrom(docPath)
			-- export thisDoc to file (pathToDestinationFolder & movName) as QuickTime movie -- with replacing
			-- Or:
			save self contained thisDoc in file (pathToDestinationFolder & movName)
		end repeat
		quit
	end tell
end main

-- Given a document path, derive a new file name with a ".mov" extension.
on makeMovNameFrom(originalPath)
	set astid to AppleScript's text item delimiters
	-- Extract the file name from the path. (On my machine, the path is a POSIX one.)
	set AppleScript's text item delimiters to "/"
	set originalName to text item -1 of originalPath
	-- Replace the existing extension with ".mov" or just append ".mov" if there isn't one.
	if (originalName contains ".") then
		set AppleScript's text item delimiters to "."
		set movName to (text 1 thru text item -2 of originalName) & ".mov"
	else
		set movName to originalName & ".mov"
	end if
	set AppleScript's text item delimiters to astid
	
	return movName
end makeMovNameFrom

Thanks, Shane. Good to know. :slight_smile:

Nigel you totally rock! This is awesome and fast!

Thanks again for all your help.

David