osascript failing when called a certain way

All

I am working on a script (BASH) that will run after a video is downloaded. It calls applescript to add the video to iTunes.

The script works when called from terminal, but, when it is called automatically from the download application SABnzbd, it fails.

the Script;

#!/bin/bash

# 1		The final directory of the job (full path)
# 2		The name of the NZB file
# 3		Clean version of the job name (no path info and ".nzb" removed)
# 4		Newzbin report number (may be empty
# 5		Newzbin or user-defined category
# 6		Group that the NZB was posted in e.g. alt.binaries.x

DIR=$1
NZB_FILE=$2
NAME=$3
NZB_ID=$4
CATEGORY=$5
GROUP=$6

StartDir="/Users/user1/HandBreak-SABnzbd"

HandBreakDir="$StartDir/HandBreakCLI"
AtomicParsleyDir="$StartDir/AtomicParsley"
iTunesAdd="/Music/iTunes/iTunes Media/Automatically Add to iTunes"

echo "Directory: "$DIR
echo "NZB Filename: "$NZB_FILE
echo "Job Name: "$NAME
echo "NZB ID: "$NZB_ID
echo "Category: "$CATEGORY
echo "Usenet Group: "$GROUP
echo ""
echo ""

# wont print error in for loop if there are no avi files in the directory
shopt -s nullglob

# regex to match job name to strip out show name, season number, episode number and episode name
# in multi part episodes (eg: Show - 1x01-1x02 - Episode), the first episode (1 in this case) is used
regex="^(.*) - ([[:digit:]])+x([[:digit:]]+).* - (.*)$"

# perform regex
if [[ $CATEGORY -eq "tv" && $NAME =~ $regex ]]; then
	show_name=${BASH_REMATCH[1]}
	season=${BASH_REMATCH[2]}
	episode=${BASH_REMATCH[3]}
	episode_name=${BASH_REMATCH[4]}
	#episode=$((episode+0))
	episode=`echo $episode | sed 's/0*//'`
fi

# navigate to directory of video file
cd "$DIR"

# iterate through all avi files
for i in *.*; do

	# if there is already an .mp4 file continue
	if [[ -e "${i%.*}"" - AppleTV.mp4" ]]; then
		echo "Skipping" $i
		continue
	fi
	
	# convert avi file to mp4 file using HandBrake
	  $HandBreakDir/HandBrakeCLI -i "$i" -o "${i%.*}"" - AppleTV.mp4" --preset="AppleTV"
	
			

	# if HandBrake did not exit gracefully, continue with next iteration (This only occurs on a MAJOR error)
	if [[ $? -ne 0 ]]; then
		continue
	fi
	
	# Check to see if the file was created if so, add it to iTunes
	if [[ -e "${i%.*}"" - AppleTV.mp4" ]]; then
	
	
	# add converted video file to itunes
	
osascript << EOT
tell application "iTunes"
	using terms from application "iTunes"
		set posix_path to "$DIR/${i%.*} - AppleTV.mp4"
		set mac_path to posix_path as POSIX file
		set video to (add mac_path)
		if "$CATEGORY" = "tv" then
			set video kind of video to TV show
			set show of video to "$show_name"
			set season number of video to "$season"
			set episode number of video to "$episode"
			set episode ID of video to "$episode_name"
		else if "$CATEGORY" = "movies" then
			set name of video to "$NAME"
		end if
	end using terms from
end tell
		
EOT

echo "${i%.*}"" - AppleTV.mp4 ---> Created and added to iTunes"

else
	echo "No Video Created ------------------------"
fi

done

exit 0

When run from terminal;

works fine. when run at the end of the download from the applications I get;

I am working off other’s code and could use some help with the above issue and;

  1. how / why does this add the video to iTunes? by referencing the file in iTunes, and changing the ATOMs it adds it?
  2. what is this;
set video to (add mac_path)

what does (add …) do that just

set video to mac_path

would not do

Thank you.

Hi,

the parameter of the add command in iTunes expects a list of alias, try


.
set mac_path to posix_path as POSIX file as alias as list
.

Not sure I can really test this script and I think the same maybe for Stefan.

But Stefan if the error kcossabo is getting is a not defined, should there not still be something defined in

set mac_path to posix_path as POSIX file

.

I suspect the problem is

set posix_path to "$DIR/${i%.*} - AppleTV.mp4"

not picking up the path for some reason.

Have you tried to log the posix_path, to see what its returning.

Thank you all. I will try both.

Quick Question. if I;

where is it logged to?

I have used the EVENT and RESULT part of the AppleScriptEditor to view ‘log’ items, but not from a BASH script calling applescript.

thanks for the guidance.

Actually I suppose You could simply insert

display dialog posix_path

So changing to

resulted in

will try the display.

thanks.

the Display, showed the correct POSIX path.

try to put the two lines, which specify the path out of the iTunes tell block.
btw: the using terms from block is actually not needed

:rolleyes:

I think I got it.

The issue is that iTunes is not accepting the video file (I can not drag and drop it using finder to iTunes).

The AppleTV file needs to be of extension m4v not mp4.

The variable undefined was that iTunes did not accept the POSIX name, as it was not a valid file???

need to figure out how to put in some error checking.

thanks all. I will let everyone know if this solves it.

The is most likely something wrong with the path?.

My iTunes accepts mp4 with no issues

So I have setup to run it in bash, as with you works fine.

Not sure how you use the SABnzbd app as I do not have a Usenet account.
How do you call the script from it.

So if the question is how to configure SABnzbd,

In the Configuration tab for Folders, there is a Post-Processing Script Folder - set it to the place of the script
Then for each BIN download, there are pul downs for Category - Set to “TV” and Script - Set to the script.

If you are asking how SABnzbd actually calls the script, I have no idea. That is above my head.

I can now confirm that the issue is the file extension.

iTunes will not accept the AppleTV encoded Video as a mp4 it needs to be a m4v

This is true for this script, dragging and dropping and the

~\Music\iTunes\iTunes-Media\Automatically Add to iTunes

if present.

The error seems to be caused from the variable being unable to keep the assignment of the POSIX path. My assumption is that it is added, then iTunes rejects it, and erases it, then the next reference sees it as ‘unassigned’

#!/bin/bash

# 1		The final directory of the job (full path)
# 2		The name of the NZB file
# 3		Clean version of the job name (no path info and ".nzb" removed)
# 4		Newzbin report number (may be empty
# 5		Newzbin or user-defined category
# 6		Group that the NZB was posted in e.g. alt.binaries.x

DIR=$1
NZB_FILE=$2
NAME=$3
NZB_ID=$4
CATEGORY=$5
GROUP=$6



echo "Directory: "$DIR
echo "NZB Filename: "$NZB_FILE
echo "Job Name: "$NAME
echo "NZB ID: "$NZB_ID
echo "Category: "$CATEGORY
echo "Usenet Group: "$GROUP
echo ""
echo ""

# wont print error in for loop if there are no avi files in the directory
shopt -s nullglob

# regex to match job name to strip out show name, season number, episode number and episode name
# in multi part episodes (eg: Show - 1x01-1x02 - Episode), the first episode (1 in this case) is used
regex="^(.*) - ([[:digit:]])+x([[:digit:]]+).* - (.*)$"

# perform regex
if [[ $CATEGORY -eq "tv" && $NAME =~ $regex ]]; then
	show_name=${BASH_REMATCH[1]}
	season=${BASH_REMATCH[2]}
	episode=${BASH_REMATCH[3]}
	episode_name=${BASH_REMATCH[4]}
	#episode=$((episode+0))
	episode=`echo $episode | sed 's/0*//'`
fi

# navigate to directory of video file
cd "$DIR"

# iterate through all avi files
for i in *.avi *.mkv; do

	# if there is already an .m4v file continue
	if [[ -e "${i%.*}"" - AppleTV.m4v" ]]; then
		echo "Skipping" $i
		continue
	fi
	
	# convert avi file to mp4 file using HandBrake - Replace /Users/user/HandBreak-SABnzbd/HandBreakCLI with you rpath to  HandBreakCLI 
	/Users/user/HandBreak-SABnzbd/HandBreakCLI/HandBrakeCLI -i "$i" -o "${i%.*}"" - AppleTV_processing.m4v" --preset="AppleTV"
	
	# if HandBrake did not exit gracefully, continue with next iteration
	if [[ $? -ne 0 ]]; then
		continue
	fi

	mv "${i%.*}"" - AppleTV_processing.m4v" "${i%.*}"" - AppleTV.m4v"

	# add converted video file to itunes
	osascript <<APPLESCRIPT
tell application "iTunes"
	set posix_path to "$DIR/${i%.*} - AppleTV.m4v"
	set mac_path to posix_path as POSIX file
	set video to (add mac_path)
	if "$CATEGORY" = "tv"
		set video kind of video to TV Show
		set show of video to "$show_name"
		-- 		
		set season number of video to "$season"
		-- 		
		set episode number of video to "$episode"
		-- 		
		set episode ID of video to "$episode_name"
	else if "$CATEGORY" = "movies"
		set name of video to "$NAME"
	end if
end tell
APPLESCRIPT

done

exit 0

In terminal.

Type: file

and then the path to the mp4 file
example:

file /Users/Username/Desktop/MVI_0434_tv.mp4

Hit enter.
what do you get?

edit**

I should add that I was using you original script with the handbrake code and mp4.
In terminal i had no problems getting a .mp4 file into iTunes.
Also drag and drop.

Also just converted a .avi file to AppleTv .m4v file with QTpro. Changed the extension to .mp4 and dragged and dropped with no issue?

Used the same script to convert a .m4v to mp4 and add to iTunes with no issues.

Did I read it wrong above that you had no issue when using your orig mp4 script in terminal??.

The only thing I am not doing is running it from SaBnzbd.

You read correctly, I changed the script though the trouble shooting, removing things like AtomicParsly, and going back to a known working state.

So I worked on the issue of the script being called getting the error from the AppleScript that Variable Video was undefined.

I added the ‘use terms from…’ and many other things to see why the script could be run from terminal and not executed from SABnzbd.

I then went back to the original source that was for iPod/iPhone, not AppleTV. The author had a newer version, that I cut and pasted. It worked for iPhone encoding.

Through many steps, I reduce down to only changing the encoding from iPhone to AppleTV (this had the mp4 extension). It Failed from SABnzbn. I did NOT re-try the terminal execution of the script, as I thought HandBreakCLI was broke.

After seeing no chatter on issues with the AppleTV profile, I from the terminal executed

I then tried to drag this file to iTunes via Finder and iTunes would no accept it. The library area turned blue, and the PLUS icon appeared, but dropping the file did not result in a copy.

I then was going to encode the same ‘somevideo.avi’ using the HandBreak GUI. There I noticed that the file extension was m4v not mp4 as the default extension. I went back to the file created with terminal execution of HandBreakCLI, and renamed it. This newly renamed file was drag’d and dropped onto iTunes with success.

I changed all .mp4 to .m4v in the script, and it is working fine from SABnzbd.

Failed attempt

and now with the extension .m4v

file adds with tag information.

thanks for all the help.

Truly odd,

I just did the

Drag and dropped, worked.

The only thing I can think is the version of iTunes?
mine is v 9.0.2 (25) on 10.6.2

O well…???

Does iTunes create a log on failure? I dislike the fact it jst does not work, verse an error log :confused:

I too have 10.6.2 and iTunes 9… I just revalidated that it failed.

I am going to try on another mac.

Any luck?

Second Mac

Mac Mini 1.4ghz PPC
OSX 10.5.8
iTune 9.0.1

Same results. Drag video file over as a .mp4 Library area turns blue, and PLUS sign appears, drop. Nothing is added. Change extension to .m4v, and repeat, file is added.

Third Mac
Mac BookPro 2.8Ghz Intel Core 2 Duo
OSX 10.6.2
iTunes 9.0.2

Same results. Required the .m4v extension

All test were done on same file. I check the file with Atomicparsley and only Atom set is @too and is Handbreak

I not used Atomicparsley before.

Do you have it tagging your file before handbrake gets is?

If so what command do you use. If I can run them on a file I have here I can see if I get what your getting.

edit*

Also what do you get if you run

in terminal.

I do not tag it before the applescript, I just used Atomicparsley to see what was tagged (just incase)

The ‘checking’ of the file is

File command produces;

same results as .mv4

*Edit - video2 is a copy of video