Mac workflow: Open source URL, v2

A probléma Leszedtél egy file-t valahonnan, a browser beletette a file metaadatai közé, hogy honnan kotortad azt elő a neten és most vissza kellene találnod a forráshoz.

A megoldás

Korábban ezt megcsináltuk Thingiverse-specifikusra, de nem tetszett, hogy a script önkényesen dönt, hogy nekem épp melyik URL kell, ezért faragtam belőle popup menüset, amivel Te választhatsz, hogy melyik metában tárolt URL-t nyissa a Safari. Lustáknak itt a bezippelt workflow, aki meg olvasgatná itt a postban a forrást az ide nézzen:
on run {input, parameters}
	set theFile to (input)
	set aFile to quoted form of POSIX path of theFile
	set theURL to (do shell script "mdls -name kMDItemWhereFroms -raw " & aFile)
	if theURL is "(null)" then -- no attribute
		set theURL to "(URL info not found)"
		set theMessage to ""
	else
		set URLs to {}
		repeat with i from 1 to the count of paragraphs of theURL
			set p to paragraph i of the theURL
			set firstQ to findFirst(p, "\"")
			set lastQ to lastOffset(p, "\"")
			if firstQ > 0 and lastQ > 0 and (lastQ - firstQ > 1) then
				copy characters (firstQ + 1) through (lastQ - 1) of p as string to q
				set end of URLs to q
			end if
		end repeat
		set selectedURL to false
		if (count of URLs) = 1 then
			set selectedURL to (item 1 of URLs)
		else if (count of URLs) > 1 then
			set selectedURL to (choose from list URLs with title "Open URL" with prompt "Select URL to open:" without multiple selections allowed)
		end if
		if (selectedURL is not false) then
			tell application "Safari"
				tell window 1
					set current tab to (make new tab with properties {URL:selectedURL as text})
				end tell
			end tell
		end if
	end if
	return input
end run
on findFirst(str, findString)
	-- HAS (http://applemods.sourceforge.net/mods/Data/String.php)
	local str, findString, len, oldTIDs
	set oldTIDs to AppleScript's text item delimiters
	try
		set str to str as string
		set AppleScript's text item delimiters to findString
		considering case
			set len to str's first text item's length
		end considering
		set AppleScript's text item delimiters to oldTIDs
		if len is str's length then
			return 0
		else
			return len + 1
		end if
	on error eMsg number eNum
		set AppleScript's text item delimiters to oldTIDs
		error "Can't findFirst: " & eMsg number eNum
	end try
end findFirst
on lastOffset(the_text, char)
	try
		set i to 1
		set last_occurrence to 0
		repeat count of the_text times
			if item i of the_text as string = char then
				set last_occurrence to i
			end if
			set i to i + 1
		end repeat
	on error
		return 0
	end try
	return last_occurrence
end lastOffset
]]>

Leave a Reply

Your email address will not be published.