Forum Archive

open_in_with_specified_activities

cvp

# This little script has been written only to show we could select which
# apps or actions we would present in the 'open in' menu.
# It allows user to select among an non exhaustive list but with
# some ones I have identified during my tests on a .gpx file.
# Of course, the list depends on the file type and on installed apps.
# Non Apple Applications of the first row (share), all return 
# 'com.apple.UIKit.activity.RemoteOpenInApplication-ByCopy' as service name, 
# thus it is not possible to identify each one while you don't know their uuid
# Each app of the 2nd row (Action) can be identified by its service.
# If you selct 'all', the script will show the standard menu.
# AirDrop has also a particular process because to exclude it, you have to 
# set it explicitely in the ExcludedActivityTypes. 
# It seems impossible to hide the 'more' icons ('autre' in French), even 
# if they are empty.
# And I don't know how to directly open a particular app without selecting it
# if this app is the only one included, like the Apple Shortcuts app does.

import ui
import time
import dialogs
from objc_util import *

def open_in_with_specified_activities(path_in,uiview,IncludedActivityTypes):
    global handler_done,SUIViewController_service
    handler_done = False
    SUIViewController_service = None
    def handler(_cmd,obj1_ptr,_error):
        global handler_done,SUIViewController_service
        # obj1_ptr = None if no service selected
        if obj1_ptr:
            obj1 = ObjCInstance(obj1_ptr)
            SUIViewController_service = str(obj1)
        handler_done = True
        return

    vo = ObjCInstance(uiview)
    SUIViewController = ObjCClass('SUIViewController')
    root_vc = SUIViewController.viewControllerForView_(vo)
    main_view = root_vc.view()
    handler_block = ObjCBlock(handler, restype=None, argtypes=[c_void_p, c_void_p, c_void_p])
    if type(path_in) is list or type(path_in) is tuple:
        path = path_in
    else:
        path = [path_in]        
    url_array = []
    for elem in path:
        url_array.append(nsurl(elem))
    UIActivityViewController = ObjCClass('UIActivityViewController').alloc().initWithActivityItems_applicationActivities_(url_array,None)

    if 'all' not in IncludedActivityTypes:
        UIActivityViewController.setExcludedActivityTypes_(['com.apple.UIKit.activity.AirDrop'])
        UIActivityViewController.setIncludedActivityTypes_(IncludedActivityTypes)

    #print(dir(UIActivityViewController))

    UIActivityViewController.setCompletionWithItemsHandler_(handler_block)

    UIActivityViewController.popoverPresentationController().sourceView = main_view
    UIActivityViewController.popoverPresentationController().sourceRect = CGRect(CGPoint(10,10), CGSize(uiview.width,uiview.height))

    root_vc.presentViewController_animated_completion_(UIActivityViewController, True, None)

    while not handler_done:
        time.sleep(1)

    if SUIViewController_service == None:
        SUIViewController_service = 'None'  # for checking at return and use in alert       

    return SUIViewController_service

v = ui.View()
v.frame = (0,0,600,400)
v.background_color = 'white'
v.present('sheet')

uuids = [
    'all',
    'com.apple.UIKit.activity.AirDrop', # AirDrop
    'com.apple.CloudDocsUI.AddToiCloudDrive', 
    'com.omz-software.Pythonista3.PythonistaAction3',
    'com.apple.UIKit.activity.Mail',
    'com.apple.UIKit.activity.CopyToPasteboard',
    'com.apple.UIKit.activity.SaveToCameraRoll',
    'com.hp.printer.control.printaction',                           
    'com.apple.UIKit.activity.AssignToContact',
    'com.readdle.PdfConverter.PdfConverterExtension',
    'com.apple.UIKit.activity.Print']
fields =[]
for uuid in uuids:
    field = {'title':uuid,'type':'switch'}
    fields.append(field)
f = dialogs.form_dialog(fields=fields)  
IncludedActivityTypes =[]
if f:
    for uuid in f.keys():
        if f[uuid]:
            IncludedActivityTypes.append(uuid)

service = open_in_with_specified_activities('sample.gpx', v, IncludedActivityTypes)
print(service)
v.close()
ccc
IncludedActivityTypes =[]
if f:
    for uuid in f.keys():
        if f[uuid]:
            IncludedActivityTypes.append(uuid)

# --->

IncludedActivityTypes = [key for key, value in f.items() if value]
cvp

@ccc Thanks, but vaue -> value 😀