Jump to content

Script Filter Icon from URL


Recommended Posts

Hey guys, quick question.

Is there any easy way to use an image from a URL as the icon for a script filter?

 

I've been downloading about 50 compressed thumbnails everytime I use a script I've been writing. (extremely slow)

Do you guys know of any easier way to get images from a URL as a script filter icon?

 

Thanks ^_^

Edited by Ritashugisha
Link to comment

Alfred won't take the url as a file source. So, make your workflow have it's proper data directory, then download the images to that directory. Then, when you're loading them, do something like -- if cached file does not exist download new file into data directory; icon url = cached file.

 

It makes it so that you'll still need to download them (there is no way around that), but it makes subsequent uses of the workflow faster.

Link to comment

Hey guys, quick question.

Is there any easy way to use an image from a URL as the icon for a script filter?

 

I've been downloading about 50 compressed thumbnails everytime I use a script I've been writing. (extremely slow)

Do you guys know of any easier way to get images from a URL as a script filter icon?

 

Thanks ^_^

 

 

Oh, if you'd rather not keep them around forever, then you might want to store them in the caches directory. That one should be cleared with every system cache clear.

 

Shawn is correct. This is the current method for using remote images as icons. Remote icons would be nice to have but it very much affects performance. The Plex workflow I had been tinkering with hehe.. it was downloading LOTS of icons from another machine on my network and it was still pretty dang slow.

Link to comment
  • 2 years later...

This is the solution I came up with. It's definitely slow on the initial load, but once a thumbnail is saved, it's pretty fast.

 

I'm using the workflows.php class:

function registerResult($result) {
	global $w;
	
	$filename = 'thumbs/' . $result["objectID"] . ".jpg";

	if(!file_exists($filename)) {
		$image = imagecreatefromjpeg($result["thumbnail"]);
		$thumb_width = 75;
		$thumb_height = 75;
		$width = imagesx($image);
		$height = imagesy($image);
		$original_aspect = $width / $height;
		$thumb_aspect = $thumb_width / $thumb_height;
		if ( $original_aspect >= $thumb_aspect )
		{
		   // If image is wider than thumbnail (in aspect ratio sense)
		   $new_height = $thumb_height;
		   $new_width = $width / ($height / $thumb_height);
		}
		else
		{
		   // If the thumbnail is wider than the image
		   $new_width = $thumb_width;
		   $new_height = $height / ($width / $thumb_width);
		}
		$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
		// Resize and crop
		imagecopyresampled($thumb,
						   $image,
						   0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
						   0 - ($new_height - $thumb_height) / 2, // Center the image vertically
						   0, 0,
						   $new_width, $new_height,
						   $width, $height);
		
		imagejpeg($thumb, $filename);
	}

	$w->result(
		$result["name"],
		$result["objectID"] . "---" . $result["canonicalUrl"] . "---" . $result["state"],
		$result["name"],
		$result["objectID"] . " - " . $result["state"] . isset($result["prices"]) ? " - " . money_format('$%i', $result["prices"]["final"]) : "",
		$filename
	);

	return;
}
Link to comment

Here is a fast(-feeling) way. Instead of waiting for missing images, it handles them in a background process and tells Alfred to re-run the Script Filter after 1 second.

 

This class generates thumbnails, but the principle is the same as caching remote URLs.

  1. Generate Alfred results and add any missing images to a queue
  2. At the end of your Script Filter, if there's a queue, start a background process to handle it
  3. If the background process is running, tell Alfred to re-run the Script Filter in a second
  4. Exit Script Filter

Obviously, the results come up image-less until the images have been cached, but it keeps the workflow super-responsive, and Alfred 3.2's re-run feature seamlessly reloads the results as the images come in.

 

Edited by deanishe
Update URL
Link to comment
  • 3 years later...

How to trigger re-run the Script Filter?

 

For example, how do I trigger in a JS.

 

On 1/26/2017 at 5:39 PM, deanishe said:

Here is a fast(-feeling) way. Instead of waiting for missing images, it handles them in a background process and tells Alfred to re-run the Script Filter after 1 second.

 

This class generates thumbnails, but the principle is the same as caching remote URLs.

  1. Generate Alfred results and add any missing images to a queue
  2. At the end of your Script Filter, if there's a queue, start a background process to handle it
  3. If the background process is running, tell Alfred to re-run the Script Filter in a second
  4. Exit Script Filter

Obviously, the results come up image-less until the images have been cached, but it keeps the workflow super-responsive, and Alfred 3.2's re-run feature seamlessly reloads the results as the images come in.

 

 

Link to comment
20 hours ago, deanishe said:


What do you mean “in a JS”?

 

You can only tell Alfred to rerun a Script Filter from its own feedback JSON.

 

What's the feedback JSON ?

 

Can you post a link or example?

 

What do you mean “in a JS”? means in a Run Script step.

 

 

Link to comment
  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...