Win XBMC FanArt Linker (for Desktop Wallpaper) - PowerShell Script
#1
Hi all,

I was looking for a way to register .tbn files as images within windows so that they could be used as wallpapers within Windows.

The closest thing I found was the brilliant idea by Stargazer to .jpg links of FanArt in another folder using PoSH, as detailed in this thread: http://forum.xbmc.org/showthread.php?tid=129876

However I ran into a couple of issues:
-I didn't want to install community extensions for New-Symlink to work
-My fanart is located in the same folder alongside each movie - so I needed recursion / extension filtering.
-Because my fanart is in a different folder, some of the filenames share the same name and wouldn't get linked in the original script -- links need to be specific to each files full path.

So I rewrote the script to do the following:
-Use mklink from cmd.exe to generate the file links instead of PSCX New-Symlink -- alternatively creates direct copies of TBN files for Windows machines not supporting mklink.
-Added variables for file extension filtering and more complex where-object filtering (strictly speaking you can get away with just extensions, but I wanted a quick way for a logical-AND type filter)
-Added a check to test for existence of each link on each run - so it will not attempt to regenerate a link. If you need to recreate fanart on each run of the script uncomment the relevant line.
-Added a check for filename length. Will attempt to reduce filenames if they are too long -- but this is probably unreliable.
-Set the link filename to be the full path to the original file with all special characters replaced with an underscore (_). This results in some pretty nasty filenames however -- recommended that you create a folder JUST for FanArt.

To use it you need:
-A Windows PC with PowerShell >=2.0 available. Recent versions of Windows come with it preloaded.
-Powershell Execution Policy set to Unrestricted or RemoteSigned.
-Knowledge on where your fanart is located and what it's called.
-A folder to dump all of your FanArt links or files in.
-The ability to read and/or edit the script configuration as required.

To use, open notepad and paste the following code in there
Code:
#XBMC FanArt to JPG Linker/Copier - bhicks 2012, based on the script by stargazer.
#Don't forget to run set-executionpolicy remotesigned in a PowerShell prompt before first run.
#See forum thread for info on running as a scheduled task.
$searchQuery = "MOVIE_DIRECTORY\*" #Path to search for Files. Note that this is will scan subdirectories as well -- you'll also want the wildcard ['*'] on the end
$searchFormats = @("*.tbn"; "*.jpg"; "*.jpeg") #Extensions for image files.
$filterBlock = { $_.BaseName -Like "*fanart*" } #Advanced filtering. Default will only look for files with 'fanart' in the name -- so it won't link thumbs etc.
$outputDir = "~\Pictures\FanArt" #Output Directory. By default it's your 'Pictures' directory [i.e C:\Users\<username>\Pictures\]
$deleteFiles = $False #Option to delete old links on startup. Disabled by default, set to $True to enable
$copyNoLink  = $False #Option to copy images instead of linking them. Useful if your PC doesn't support mklink -- but will take up more space on disk and slow the script down. Set to $True to enable

#That's the configuration done! You shouldn't need to edit anything else, unless you want to.
$images = Get-ChildItem $searchQuery -Recurse -Include $searchFormats | where-object $filterBlock
$invalidFileChars = [io.path]::GetInvalidFileNamechars()
$invalidFileChars += '['
$invalidFileChars += ']'
$invalidFileCharsRegex = [string]::join('|', ($invalidFileChars | foreach-object { [regex]::escape($_) } ) )

$outputDir = Resolve-Path $outputDir
if ( !($outputDir.ToString().EndsWith('\') ) )
{
    $outputDir = "$outputDir\"
}

$maxBaseNameLength = 260 - $outputDir.Length - ".jpg".Length
if ($maxBaseNameLength -le 0)
{
    write-Host ("WARNING: Exiting script run, output path too long for Windows filenames - shorten outputDir?")
    exit
}

cd $outputDir

if ($deleteFiles)
{
    get-childitem | remove-item
}

if ($images -ne $NULL)
{
    foreach ($image in $images)
    {
        #Recall maximum length of FullPath is 260 characters - work out how many characters we have remaining for actual filename without extension
        $outputBaseName = $image.FullName -Replace $invalidFileCharsRegex, "_"
        $outputFile =  ($outputBaseName.Substring(0, [System.Math]::Min($maxBaseNameLength, $outputBaseName.Length))) + ".jpg"
        $imagePath = $image.FullName
        if ( !(Test-Path -LiteralPath $outputFile))
        {
            if ($copyNoLink)
            {
                Copy-Item $imagePath .\$outputFile
                Write-Host "Copied $imagePath ===>> $outputDir$outputFile succesfully."
            }
            else
            {
                cmd.exe /c mklink "`"$outputFile`" `"$imagePath`""
            }
        }
    }
}

Edit the start of the script to match your configuration. At the very least you will want to change the searchQuery to point to the folder where you fanart is located (replace MOVIE_DIRECTORY with the path to the root of your movie folder). In my case this was E:\* -- you will want to keep the star (wildcard) on the end. This is a small caveat with the way the images are located.

You may also want to change $outputDir to something else, although the default will be suitable for most users.

Change the other options as you wish -- I hope they are fairly self explanatory. Remember to keep the double quotes intact around each value.

Save the file somewhere that you'll remember -- say C:\Programs\XBMC-FanArt-Linker.ps1.

Now create the $outputDir folder that you specified in the script. If you didn't change $outputDir this will be %USERPROFILE%\Pictures\FanArt (e.g. create a FanArt folder in your Pictures Library).

Next, we will perform an initial run of the program to make sure everything is configured properly.

To do this, open up the start menu and start a PowerShell session (Typically Start->All Programs->Accessories->Windows PowerShell->Windows PowerShell).You should see a blue box with a prompt.
Now type set-executionpolicy remotesigned followed by <Enter>. You will be asked to confirm - Press Y and then enter again. This will configure PowerShell to allow the execution of scripts; it only needs to be done once.

Next we will run the script itself. To do this type in a dot (.) followed by space and then the path to the script file you saved -- e.g. ". C:\Programs\XBMC-FanArt-Linker.ps1" and then <Enter> again.

If everything has been setup correctly you will see some lines saying that symbolic links have been created or that files have been copied successfully -- if you see any red lines of text you have an error, check your configuration for typos and/or reply to this thread.

Check your $outputDir -- you should see a heap of fanart posters! You can now configure your wallpaper or screensaver etc. to use this folder as its image source.

Now that you've got it all working you'll probably want to create a scheduled task to run this at specified intervals - as it won't automatically detect new data.

To do so, create a scheduled task via the scheduled tasks manager, set the schedule how you want it, and use the following as the action:
Action: Start a Program
Program: PowerShell.exe
Arguments: -WindowStyle Minimized SCRIPT_PATH

Where SCRIPT_PATH is the path to the file you saved earlier on - for example C:\Programs\XBMC-FanArt-Linker.ps1

I realize that this would be far better off as a separate (cross-platform) app but to be honest that's more trouble than I care to give it -- I knocked this up as a quick solution to something I'd been looking at for a while and figured I'd share it with anyone else who might be interested.

Hopefully it's of some use to someone out there.
Reply

Logout Mark Read Team Forum Stats Members Help
XBMC FanArt Linker (for Desktop Wallpaper) - PowerShell Script0