Category Archives: Code

Postfix PCRE TLD body check

This is used to reject spam e-mail containing a URL to the top level domains .date or .loan, and can of course be modified to include others. If you feel your users may ever have a legitimate need to be contacted about these domains it would be best to not use this.

Some familiarity with perl compatible regular expressions in postfix will be helpful.

/https?:\/\/([a-z,0-9,-]+\.){1,3}(date|loan)\// REJECT

This pattern matches both http and https protocols, and links containing from 1 to 3 subdomains of the TLD (http://3.2.example.com/ or https://example.com/ would match were .com the target).

Insert that line into your custom body checks file and no more spam du jour. Enjoy.

Parse e-mail for UPS tracking numbers

This AppleScript will parse the selected e-mail in Mail.app for UPS tracking numbers and will open the tracking page of the UPS website in your default browser.

This only looks at a single e-mail, but could be made to repeat through multiple selections.

tell application "Mail"
set _messages to selection as list
set input to content of item 1 of _messages
try
open location my parse_nums(input)
on error
display dialog "No UPS tracking info found."
end try
end tell

on parse_nums(input)
set input_new to ""
set output to ""
set input to every word of input
repeat with i in input
try
if first character of i is "1" and second character of i is "Z" then --Every UPS # I've seen starts with '1Z'
set input_new to input_new & i & "%0D%0A" as string --
URL encoded carriage return line feed needed for multiple entries.
end if
end try
end repeat
-- Now to remove the last set of new line characters.
repeat with c in characters 1 through ((count of characters of input_new) - 6) of input_new
set output to output & c
end repeat
set the_URL to "https://wwwapps.ups.com/WebTracking/track?track.x=Track&trackNums=" & output
return the_URL
end parse_nums

Set creation and modification date

This is to correct file times on photos that were imported from cameras with bad date settings or when they have been transferred to-and-fro and picked up the wrong data some where or after re-encoding high bit rate video to save a few GBs. [This does not alter the date atoms encoded in (most?) video files. ffmpeg can do that, however.]
Continue reading

OS X Server 5 Shenanigans

OS X Server 5 for OS X 10.10. and 10.11, while a significant improvement to Lion Server for OS X 10.7, still behaves oddly when mixing SSL and non-SSL requests. Whereas before it wanted every request redirected to the SSL pipe, now it will let you do either. It won’t, however, let you do both.

WordPress will try to do both. Why? I dunno. It should work, and did on plain Apache 2, but OS X Server 5 puts up a proxy to the WAN and serves from a different port. This is not presented upfront to the Server.app user, but is most likely in the docs. RTFM much? Ha-ha! No.

The affect this had on my WordPress install is instead of logging in being presented with a page that merely stated,

You don’t have permission to view this page.

Or some such equally helpful information. A little web searching turned up the answer. To overcome this the following can be added to wp-includes/http.php

/* Adding this BS for OS X Server 5 being a dick... */
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS']='on';
}

I saw a post on wordpress.net that said to put this into config.php, but that did not work for me. Neither did the mod_rewrite mumbo-jumbo in that thread and apparently it didn’t for the OP, either.

I admit being ignorant to what HTTP_X_FORWARDED_PROTO is, but it appears to be a HTTP header from an Apache mod Apple is running. Unique to them or something well known? I dunno. I don’t pretend to try to keep up with this stuff any longer. That’s why I went to WordPress; So I wouldn’t have to.

iTunes Content Rating strings

Re: http://sourceforge.net/projects/atomicparsley/forums/forum/514419/topic/1687885?message=4206988

United States (01)

Not Rated mpaa||0|

G mpaa|G|100|

PG mpaa|PG|200|

PG-13 mpaa|PG-13|300|

R mpaa|R|400|

NC-17 mpaa|NC-17|500|

Unrated mpaa|UNRATED|900|

Not Rated us-tv||0|

TV-Y us-tv|TV-Y|100|

TV-Y7 us-tv|TV-Y7|200|

TV-G us-tv|TV-G|300|

TV-PG us-tv|TV-PG|400|

TV-14 us-tv|TV-14|500|

TV-MA us-tv|TV-MA|600|

Unrated us-tv|UNRATED|900|

These alone (added using a hex editor) are not enough by themselves to get iTunes to see the rating. Not sure at all what is missing.

Save list of folder contents

[UPDATE] Revisited this one and made it a little nicer. It is faster and gives a total count of directories and files, skips invisibles (but you can undo that), and doesn’t error on item names longer than 31 characters. Confirmed to run on 10.4, YMMV.

This AppleScript will create a file containing a tree style list of all files inside the chosen directory and save that list in a file created in that directory. The list file will include the path of the listed directory as well as the date and time it was saved and a tally of me bananas… er, folders and files under the starting directory.

The default directory in the choose dialog is the folder of the Finder’s frontmost window unless there are no Finder windows. In which case the default is the Desktop folder.

It does not follow aliases (not sure about symlinks) and will indicate an alias by placing ” [alias]” behind the file name.

This is actually a modified version of a script that’s been around the web for years.

-- http://strawhousepig.net/

set fileList to ""
set d_count to 0
set f_count to 0
global fileList, d_count, f_count

on listFolder(f, s)
list folder f as alias without invisibles
repeat with i in the result
set finfo to (info for alias (f & i))
if folder of finfo then
set d_count to d_count + 1
set fileList to fileList & s & i & "/" & return
listFolder(f & i & ":", s & " ")
else
if alias of finfo then
set f_count to f_count + 1
set fileList to fileList & s & i & " [alias]" & return
else
set f_count to f_count + 1
set fileList to fileList & s & i & return
end if
end if
end repeat
return {fileList, d_count, f_count}
end listFolder

try
tell application "Finder" to set cwd to (POSIX path of (folder of window 1 as string)) as POSIX file
on error
set cwd to path to desktop folder
end try
set theFolder to (choose folder "Select a folder to list:" default location cwd) as string

--This will get the name of the folder for use in the name of the generated file
set folderName to name of (info for (theFolder as alias))

--This will get the Unix style path of the folder for info purposes inside the file itself
set folderPath to POSIX path of theFolder

set infoList to listFolder(theFolder, "")
set fileList to "# File list of: " & folderPath & " on " & (current date) & return & item 2 of infoList & " folders and " & item 3 of infoList & " files total." & return & return & item 1 of infoList

set listFile to ((theFolder) as text) & "!File list of this folder (" & folderName & ")" & (do shell script "date \"+ %Y-%m-%d %H%M\"") & ".txt"

tell application "Finder"
set newFile to (open for access file listFile with write permission)
set eof newFile to 0
write fileList to newFile
close access newFile
open listFile as alias
end tell

iPhoto import from folders AppleScript

Preface: Having upgraded my phone (Nokia N8) firmware to “Belle” — whatever version that is, the Nokia Multimedia Transfer application no longer imports photos to iPhoto.* Tragic because the camera is the sole reason I bought this phone. Ordinarily this would not be a problem since the phone uses a “DCIM” folder at the drive root and can store images with 8 character names (5 of which you set yourself). But not every photo app does store them there or with a “proper” name. Even though there exists an “Images” folder in the drive root that I know gets used, Mass Storage.app does not access it. Could be another naming convention similar to DCF

This is an AppleScript to get around this limitation, and to allow (in my case at least) the importing of camera files named by date. In short you choose which folders you want this script to tell iPhoto to import from and can be used with any folder(s), no specific device required.

Notable: Uses ‘defaults’ to store and load folder paths in a property list (.plist) file.
Continue reading