import sys
import os
import urlparse
from pocket import Pocket
from workflow import Workflow

CONSUMER_KEY = '25349-924436f8cc1abc8370f02d9d'


def main(wf):
    if is_running('Google Chrome'):
        add_item(get_Chrome_item())
        print "Chrome link added to Pocket"
        wf.clear_cache()
        return 0
    elif is_running('Safari'):
        add_item(get_Safari_item())
        print "Safari link added to Pocket"
        wf.clear_cache()
        return 0
    else:
        item = get_clipboard_item()

        if item is not None:
            add_item(item)
            print "Clipboard link added to Pocket"
            wf.clear_cache()
            return 0

        print "No link found!"
        return 0


def is_running(app):
    return os.popen("""osascript -e 'tell app "System Events" to count processes whose name is "%s"' """ % app).read().rstrip() == "1"


def get_Chrome_item():
    url = os.popen(
        """ osascript -e 'tell application "Google Chrome" to return URL of active tab of front window' """).readline()
    title = os.popen(
        """ osascript -e 'tell application "Google Chrome" to return title of active tab of front window' """).readline()
    return {
        'url': url,
        'title': title
    }


def get_Safari_item():
    url = os.popen(
        """ osascript -e 'tell application "Safari" to return URL of front document' """).readline()
    title = os.popen(
        """ osascript -e 'tell application "Safari" to return name of front document' """).readline()
    return {
        'url': url,
        'title': title
    }


def get_clipboard_item():
    clipboard = os.popen(""" osascript -e "get the clipboard" """).readline()
    parts = urlparse.urlsplit(clipboard)
    if not parts.scheme or not parts.netloc:
        return None
    return {
        'url': clipboard,
        'title': None
    }


def add_item(item):
    access_token = wf.get_password('pocket_access_token')
    pocket_instance = Pocket(CONSUMER_KEY, access_token)
    pocket_instance.add(url=item['url'], title=item['title'], tags="alfred")


if __name__ == '__main__':
    wf = Workflow()
    sys.exit(wf.run(main))

@@ -158,27 +158,35 @@
 %0A    if 
-is_running(
+frontmost_app() == 
 'Google 
@@ -192,17 +192,16 @@
  Chrome'
-)
 :%0A      
@@ -329,27 +329,35 @@
 lif 
-is_running(
+frontmost_app() == 
 'Safari'
 ):%0A 
@@ -352,17 +352,16 @@
 'Safari'
-)
 :%0A      
@@ -731,16 +731,228 @@
 urn 0%0A%0A%0A
+def frontmost_app():%0A    return os.popen(%22%22%22 osascript -e 'tell application %22System Events%22' -e 'set frontApp to name of first application process whose frontmost is true' -e 'end tell' %22%22%22).readline().rstrip()%0A%0A
 def is_r
@@ -2371,8 +2371,9 @@
 (main))%0A
+%0A

Use frontmost application instead of running application.